JSP 说helloworld
来源:互联网 发布:源代码公开的软件 编辑:程序博客网 时间:2024/05/17 01:18
这是我学习JSp的helloworld
由于初学就不说原理了,直接来上代码!
1整体架构如下:
2 user.java代码
package com.syw;public class User {private String username;public String getUsername() {return username;}public void setUsername(String username) {this.username = username;}}
3 userserlevt.java 代码
package com.syw;import java.io.IOException;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import com.sun.jersey.api.core.HttpResponseContext;import com.sun.jersey.spi.dispatch.RequestDispatcher;public class Userservlet extends HttpServlet {protected void doget(HttpServletRequest req, HttpServletResponse resp)throws ServletException, IOException{doPost(req, resp);String username = req.getParameter("username");}protected void doPost(HttpServletRequest req, HttpServletResponse resp)throws ServletException, IOException{String usname = req.getParameter("username"); //取得输入信息User user = new User();user.setUsername(usname);req.setAttribute("user", user); //将对象添加到HttpServletRequest 对象中javax.servlet.RequestDispatcher rdt = req.getRequestDispatcher("/index.jsp");//将请求转向指定页面((javax.servlet.RequestDispatcher) rdt).forward(req, resp);}}
4 index.jsp
<%@ page contentType="text/html; charset=gb2312"%><%@ page language="java" import="java.util.*,com.syw.*"pageEncoding="UTf-8"%><%String path = request.getContextPath();String basePath = request.getScheme() + "://"+ request.getServerName() + ":" + request.getServerPort()+ path + "/";%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html><head><meta http-equiv="Content-Type" content="text/html; charset=gb2312"><base href="<%=basePath%>"><title>load</title><meta http-equiv="pragma" content="no-cache"><meta http-equiv="cache-control" content="no-cache"><meta http-equiv="expires" content="0"><meta http-equiv="keywords" content="keyword1,keyword2,keyword3"><meta http-equiv="description" content="This is my page"><!--<link rel="stylesheet" type="text/css" href="styles.css">--><script language="javaScript">//首先判断输入的是否是空值,不是则提交function checkstr() {if (document.getElementById("CustomerID").value == "") {alert("用户名不能为空!");return false;}if (document.getElementById("CompanyName").value == "") {alert("密码不能为空!");return false;}form.submit();}</script></head><body><%User user = (User) request.getAttribute("user");if (user != null && user.getUsername() != null) {if (user.getUsername().equals("")) {out.println("请输入一个用户名");} else {%>你好:<%=user.getUsername()%><%}}%><form action="index" method="post"> 用户名:<input type="text"name="username" value=""> <br><input type="submit" name="submit" value="submit" onclick="checkstr()"></form></body></html>
5 wed.xml(理解很浅,不过感觉挺重要的)
<?xml version="1.0" encoding="UTF-8"?><web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0"> <display-name>TEST</display-name> <servlet> <description>JAX-RS Tools Generated - Do not modify</description> <servlet-name>userservlet</servlet-name> <servlet-class>com.syw.Userservlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>userservlet</servlet-name> <url-pattern>/index</url-pattern> </servlet-mapping></web-app>
OK 运行效果:
主要代码给出来了哈!什么配置tomcat jdk 就不要问我了,还是google吧!
5 0
- JSP 说helloworld
- JSP---HelloWorld!
- Jsp-helloworld
- JSP-helloWorld
- 从helloworld说运行机制
- DRP之JSP HelloWorld
- jsp, servlet之helloworld
- 从零开始用Spring说helloworld--1
- 从零开始用Spring说helloworld--2
- 从零开始用Spring说helloworld--3
- 从零开始用Spring说helloworld--4
- eclipse开发jsp之HelloWorld
- jsp自定义标签之helloworld
- jsp自定义标签的HelloWorld
- [Firefox][扩展系列][02]向Firefox说声HelloWorld
- [Firefox][扩展系列][02]向Firefox说声HelloWorld
- 关于jsp找工作,前辈来说说。
- ArcGIS Server JavaScript API+Jsp入门实例(HelloWorld.jsp)
- different clocks
- tactic 快速设置和迁移的构想
- 替换文本中的空格和换行
- 系统调用的过程
- 验证码实现方法
- JSP 说helloworld
- 寒假到了,我要做一个acmer~~~~~
- git下载代码
- 文件创建、复制、移动、删除
- Unicode 字符编码表|汉字Unicode编码的区间为:0x4E00→0x9FA5
- Linux下用于查看系统当前登录用户信息的4种方法
- opencv Algorithm的getList用法
- 快播大屏幕作为快播的第一款硬件产品
- Note for Linux