- servlet概述
servlet是服务器端的java应用程序。是客户端请求与服务器响应的中间桥梁。
- servlet生命周期
- 初始化,调用init();
- 响应客户端请求,调用service();
- 销毁,调用destroy();
- HelloWorld
- 页面index.jsp.用于发送请求的客户端页面
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="ServletDemo" method="post">
<table>
<tr>
<td>用户名:</td>
<td><input type="text" name="username"></input></td>
</tr>
<tr>
<td>密 码:</td>
<td><input type="password" name="password"></input></td>
</tr>
<tr>
<td><input type="submit" value="提交"></input></td>
</tr>
</table>
</form>
</body>
</html>
2.页面result.jsp.用于显示响应结果的客户端页面
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
您的用户名:<%=request.getAttribute("username")%><br>
您的密码:<%=request.getAttribute("password")%>
</body>
</html>
3. HelloWorld.java.仅仅用于Console显示
/**
* Cappuccino's Class File
*/
package com.aaron.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* @author Cappuccino
* 2012-6-10下午12:53:57
*/
public class HelloWorld extends HttpServlet {
/**
*
*/
private static final long serialVersionUID = 2114610983564440107L;
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
doPost(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
System.out.println("servlet doPost!");
PrintWriter out = resp.getWriter();
out.print("Servlet HelloWorld");
}
}
4. ServletDemo.java。用于响应index.jsp页面的请求,将返回的结果返回到result.jsp页面。
/**
* Cappuccino's Class File
*/
package com.aaron.servlet;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* @author Cappuccino
* 2012-6-10下午05:02:01
*/
public class ServletDemo extends HttpServlet {
/**
*
*/
private static final long serialVersionUID = -4392447839946325219L;
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
doPost(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
String username = req.getParameter("username");
String password = req.getParameter("password");
//保存参数用于页面显示
req.setAttribute("username", username);
req.setAttribute("password", password);
//页面跳转
req.getRequestDispatcher("result.jsp").forward(req,resp); //传值跳转
// resp.sendRedirect("result.jsp");//不传值跳转
//控制台打印
System.out.println("username:"+username);
System.out.println("password:"+password);
}
}
5 . web.xml.配置servlet。
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>Servlet-leaning</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>HelloWorld</servlet-name>
<servlet-class>com.aaron.servlet.HelloWorld</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>HelloWorld</servlet-name>
<url-pattern>/helloworldPage</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>ServletDemo</servlet-name>
<servlet-class>com.aaron.servlet.ServletDemo</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ServletDemo</servlet-name>
<url-pattern>/ServletDemo</url-pattern>
</servlet-mapping>
</web-app>
参考:
1.http://baike.baidu.com/view/25169.htm
2.http://java.sun.com/j2ee/tutorial/1_3-fcs/doc/Servlets.html