Servlet模型
【表单的处理】【模型(1)】
●HTML网页
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<p>表单的处理</p>
<form method="post" action="ss">
用户名:<input type="text" name="name">
密码:<input type="password" name="pass">
<input type="submit" value="提交">
</form>
</body>
</html>
●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"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>Test1</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>ser1</servlet-name>
<servlet-class>cm.zhiqi.serv.ser1</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ser1</servlet-name>
<url-pattern>/ss</url-pattern>
</servlet-mapping>
</web-app>
●类的处理
package cm.zhiqi.serv;
import ...;
public class ser1 extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws
ServletException, IOException {
String name=req.getParameter("name");
String password=req.getParameter("pass");
System.out.println("name->"+name);//控制台输出信息
System.out.println("password->"+password);//控制台输出信息
resp.getWriter().write("name"+name+"password"+password);//返回到网页的
内容
}
}
运行:
【表单的处理】【模型(2)】
●HTML网页
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<p>form processing</p>
<form method="post" action="th">
<input type="hidden" name="hidden" value="test" />
Sex:<input type="radio" name="xingbie" value="m" checked/>man
<input type="radio" name="xingbie" value="f" />woman<br><br>
Coursr:<input type="checkbox" name="course" value="PHP" />PHP
<input type="checkbox" name="course" value="Java" />Java
<input type="checkbox" name="course" value="C#.NET" />C#.NET<br><br>
City<select name="city">
<option value="zibo">淄博</option>
<option value="jinan">济南</option>
<option value="qingdao">青岛</option>
<option value="weihai">威海</option>
<option value="rizhao">日照</option>
</select>
<br><br>
<input type="reset" value="取消">
<input type="submit" value="提交">
</form>
</body>
</html>
●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" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>Test2</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>Test2</servlet-name>
<servlet-class>az.zhiqi.Test2</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Test2</servlet-name>
<url-pattern>/th</url-pattern>
</servlet-mapping>
</web-app>
●类的处理
package az.zhiqi;
import ...;
public class Test2 extends HttpServlet {
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req.setCharacterEncoding("utf-8");
String hidden=req.getParameter("hidden");
String xingbie=req.getParameter("xingbie");
String [] course=req.getParameterValues("course");
String city=req.getParameter("city");
city=new String(city.getBytes("ISO8859-1"),"utf-8");
resp.getWriter().write("hidden->"+hidden+"<br>xingbie->"+xingbie+"<br>course->"+Arrays.toString(course)+"<br>city->"+city);
}
}
暂时不用汉字,字符集问题暂时不考虑!
运行如下:
【表单的处理】【模型(3)】
forword
●HTML网页
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<a href="s1">ser</a>
</body>
</html>
●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" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>Test3</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>s1</servlet-name>
<servlet-class>zm.zhiqi.al.ty1</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>s1</servlet-name>
<url-pattern>/s1</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>s2</servlet-name>
<servlet-class>zm.zhiqi.al.ty2</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>s2</servlet-name>
<url-pattern>/s2</url-pattern>
</servlet-mapping>
</web-app>
●类的处理
package zm.zhiqi.al;
import java.io.IOException;
import ...;
public class ty1 extends HttpServlet {
private static final long serialVersionUID = 1L;
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("doGet");
//s1转s2处理
RequestDispatcher rd=req.getRequestDispatcher("s2");
req.setAttribute("test", Math.random());
rd.forward(req, resp);
}
}
package zm.zhiqi.al;
import java.io.IOException;
import ...;
public class ty2 extends HttpServlet {
private static final long serialVersionUID = 1L;
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("Other doGet");
double d=(Double)req.getAttribute("test");
resp.getWriter().write(" "+d+" ");
resp.getWriter().write("other processing");
}
}
【表单的处理】【模型(4)】
include
●HTML网页
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<a href="s1">ser</a>
</body>
</html>
●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" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>Test3_B</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>s1</servlet-name>
<servlet-class>zm.zhiqi.al.t1</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>s1</servlet-name>
<url-pattern>/s1</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>s2</servlet-name>
<servlet-class>zm.zhiqi.al.t2</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>s2</servlet-name>
<url-pattern>/s2</url-pattern>
</servlet-mapping>
</web-app>
●类的处理
package zm.zhiqi.al;
import java.io.IOException;
import ...;
public class t1 extends HttpServlet {
private static final long serialVersionUID = 1L;
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("doGet");
RequestDispatcher rd=req.getRequestDispatcher("s2");
req.setAttribute("test", Math.random());
rd.include(req, resp);
double d=(Double)req.getAttribute("test");
resp.getWriter().write("include->"+d+"<-include");
}
}
package zm.zhiqi.al;
import java.io.IOException;
import ...;
public class t2 extends HttpServlet {
private static final long serialVersionUID = 1L;
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("Second doGet");//往控制台输出
//double d=(Double)req.getAttribute("test");
//resp.getWriter().write(" "+d+" ");
resp.getWriter().write("Second processing<br>");//向网页输出
}
}
【表单的处理】【模型(5)】
Session会话