一.制作一个简单的servlet:
(1)建立web.xml;
<servlet-mapping>只是一个简单的容器,
<servlet-name>Sample</servlet-name>表示要访问的servlet文件名,
<url-pattern>/servlet/Sample</url-pattern>表示的是要访问Sample.java的路径
<servlet>
<description>第一个例子</description>
<display-name>servletSample</display-name>
<servlet-name>Sample</servlet-name>servlet名字
<servlet-class>jp.co.nttdata.servlet.Sample</servlet-class>类所在包及类名
<?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_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>servlet</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<servlet>
<description>第一个例子</description>
<display-name>servletSample</display-name>
<servlet-name>Sample</servlet-name>
<servlet-class>jp.co.nttdata.servlet.Sample</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Sample</servlet-name>
<url-pattern>/servlet/Sample</url-pattern>
</servlet-mapping>
<servlet>
<description>第二个例子</description>
<display-name>servletJspSample</display-name>
<servlet-name>JspSample</servlet-name>
<servlet-class>jp.co.nttdata.servlet.JspSample</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>JspSample</servlet-name>
<url-pattern>/servlet/JspSample</url-pattern>
</servlet-mapping>
</web-app>
(2)建立包jo.co.nttdata.servlet和类Sample,JspSample:
JspSample:
package jp.co.nttdata.servlet;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class JspSample extends HttpServlet {
/**
* serialVersionUID
*/
private static final long serialVersionUID = 1751356779488685371L;
public void init() throws ServletException {
System.out.println("This is initMethod");
}
public void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
System.out.println("This is doGet");
resp.setContentType("text/html;charset=UTF-8");
String loginId = req.getParameter("loginId");
req.setAttribute("loginId", loginId);
req.setAttribute("loginName", "nttdata");
req.getRequestDispatcher("/response.jsp").forward(req, resp);
}
public void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
doGet(req, resp);
}
public void destroy() {
super.destroy();
System.out.println("This is destroy");
}
}
Sample:
package jp.co.nttdata.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;
public class Sample extends HttpServlet {
/**
* serialVersionUID
*/
private static final long serialVersionUID = -1936901196555528367L;
public void init() throws ServletException {
System.out.println("This is initMethod");
}
public void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
System.out.println("This is doGet");
resp.setContentType("text/html;charset=UTF-8");
PrintWriter out = resp.getWriter();
out.println("<html>");
out.println("<body>");
out.println("This is servlet");
out.println("</body>");
out.println("</html>");
}
public void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
doGet(req, resp);
}
public void destroy() {
super.destroy();
System.out.println("This is destroy");
}
}
(3)建立请求页面index.jsp和相应页面response.jsp
页面简单的制作了一个表单
<%@ page language="java" contentType="text/html; charset=windows-31j" pageEncoding="windows-31j"%>
标明了编码格式采用的编码语言
index:
<%@ page language="java" contentType="text/html; charset=windows-31j"
pageEncoding="windows-31j"%>
<!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=windows-31j">
<title>handsome</title>
</head>
<body>
<form name="loginForm" action="/servlet/servlet/JspSample" method="POST">
index
<input name="loginId" type="text" />
<input name="loginName" type="text" value="${loginName}"/>
<input type="submit" value="sub" />
</form>
</body>
</html>
response:
<%@ page language="java" contentType="text/html; charset=windows-31j"
pageEncoding="windows-31j"%>
<!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=windows-31j">
<title>Insert title here</title>
</head>
<body>
<form name="loginForm" action="/servlet/servlet/JspSample" method="POST">
response
<input name="loginId" type="text" />
<input name="loginName" type="text" value="${loginName}"/>
<input type="submit" value="sub" />
</form>
</body>
</html>