一、获取客户端信息
1.建立一个动态web (Dynamic Web project)ServletTest的项目
2 在工程.ServletTest中创建一个BookInformationServlet.java的Servlet。
package com;
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;
/**
* Servlet implementation class bookinformation
*/
public class BookInformationServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public BookInformationServlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
response.setContentType("text/html;charset=UTF-8");
PrintWriter out=response.getWriter();
request.setCharacterEncoding("UTF-8");
String price=request.getParameter("price");
out.println("<html>");
out.println("价格:"+price+"<br>");
out.println("</html>");
}
}
<%@ 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=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<form name="form1" method="post" action="BookInformationServlet">
<table>
<tr>
<td> 价格:<input name="price" type="text" id="name">
</td>
</tr>
</table>
</form>
</body>
</html>
<?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>ServletTest</display-name>
<servlet>
<servlet-name>SampleServlet</servlet-name>
<servlet-class>com.SampleServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>SampleServlet</servlet-name>
<url-pattern>/SampleServlet</url-pattern>
</servlet-mapping>
<servlet>
<description></description>
<display-name>BookInformationServlet</display-name>
<servlet-name>BookInformationServlet</servlet-name>
<servlet-class>com.BookInformationServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>BookInformationServlet</servlet-name>
<url-pattern>/BookInformationServlet</url-pattern>
</servlet-mapping>
二、Servlet之间通信
实现转发步骤:
1、得到RequestDispatcher引用:
public abstract RequestDispatcher getRequestDispatcher(String urlpath)
其中urlpath为要转发Jsp页面或Servlet地址。
RequestDispatcher dispatcher=request. getRequestDispatcher(a.jsp);
或
RequestDispatcher dispatcher;
dispatcher =getSeveltContext().getRequestDispatcher(/SecondServlet);
2、转发:
public abstract void forward(ServletRequest request,SevletResponse response)
throws ServletException,IOException
该方法用来把一个Jsp或Servlet请求提交到另一个。当其输出完全由第二个Servlet或被调用Servlet生成时,必须用此方法。若第一个Servlet已访问了PrintWriter对象,则由此方法引发异常
dispatcher.forward(request, response);
<%@ page contentType="text/html;charset=GB2312" %>
<HTML>
<BODY ><Font size=4>
<form action="Verify" method="post">
输入姓名:<Input Type=text name=name>
<BR>输入分数:<Input Type=text name=score>
<BR><Input Type=submit value="提交">
</form>
</Font>
</BODY>
</HTML>
package com;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class Verify extends HttpServlet
{ public void init(ServletConfig config) throws ServletException
{super.init(config);
}
public void doPost(HttpServletRequest request,HttpServletResponse response)
throws ServletException,IOException
{ String name=request.getParameter("name"); //获取客户提交的信息
String score=request.getParameter("score");
if(name.length()==0||name==null)
{ response.sendRedirect("sixth_example5.jsp"); //重定向
}
else if(score.length()==0)
{ response.sendRedirect("sixth_example5.jsp"); //重定向
}
else if(score.length()>0)
{ try { int numberScore=Integer.parseInt(score);
if(numberScore<0||numberScore>100)
{ response.sendRedirect("sixth_example5.jsp");
}
else
{ RequestDispatcher dispatcher=
request.getRequestDispatcher("ShowMessage");
dispatcher.forward(request, response); // 转发
}
}
catch(NumberFormatException e)
{ response.sendRedirect("input.jsp");
}
}
}
public void doGet(HttpServletRequest request,HttpServletResponse response)
throws ServletException,IOException
{ doPost(request,response);
}
}
ShowMessage.java
package com;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class ShowMessage extends HttpServlet
{ public void init(ServletConfig config) throws ServletException
{super.init(config);
}
public void doPost(HttpServletRequest request,HttpServletResponse response)
throws ServletException,IOException
{ response.setContentType("text/html;charset=utf-8");
PrintWriter out=response.getWriter();
String name=request.getParameter("name"); //获取客户提交的信息
String score=request.getParameter("score"); //获取客户提交的信息
try{ byte bb[]=name.getBytes("utf-8");
name=new String(bb,"utf-8");
}
catch(Exception exp){}
out.print("<Font color=blue size=4>你的姓名是:");
out.print(name);
out.print("<BR><Font color=pink size=4>你的成绩是:");
out.print(score);
}
public void doGet(HttpServletRequest request,HttpServletResponse response)
throws ServletException,IOException
{ doPost(request,response);
}
}