思路

查看代码注解

效果

提交表单

转发(带参)

重定向(不能带参,重新定位另外页面)

代码

class1

package Forward;
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 forward01 extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//设置字符编码
		request.setCharacterEncoding("utf-8");
		//获取请求参数
		String username= request.getParameter("username");
		String password= request.getParameter("password");
		request.setAttribute("username",username);
		request.setAttribute("password", password);
		//转发(带参数转发)
		request.getRequestDispatcher("other").forward(request, response);
		//重定向(不传参数)
//			response.sendRedirect("other");
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	doGet(request, response);
}
}

class2

package Forward;
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 other extends HttpServlet {
private static final long serialVersionUID = 1L; 
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//获取转发过来胡数据
		String username=(String)request.getAttribute("username");
		String password=(String)request.getAttribute("password");
		//设置字符码
		response.setContentType("text/html;charset=utf-8");
		//在浏览器中显示你提交胡信息
		PrintWriter out = response.getWriter();
		out.println("用户登陆信息<br>");
		out.println("========================<br>");
		out.println("useranme: "+username+"<br>");
		out.println("password: "+password+"<br>");		
		out.println("=======================<br>");
		out.println("谢谢您的光临!<br>");
}	
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	doGet(request, response);
}
}

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_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>forward01</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>forward01</display-name>
  <servlet-name>forward01</servlet-name>
  <servlet-class>Forward.forward01</servlet-class>
</servlet>
<servlet-mapping>
  <servlet-name>forward01</servlet-name>
  <url-pattern>/forward01</url-pattern>
</servlet-mapping>
<servlet>
  <description></description>
  <display-name>other</display-name>
  <servlet-name>other</servlet-name>
  <servlet-class>Forward.other</servlet-class>
</servlet>
<servlet-mapping>
  <servlet-name>other</servlet-name>
  <url-pattern>/other</url-pattern>
</servlet-mapping>
</web-app>

html

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="forward01" method="post">
username:<input name="username" type="text">
<br>
password:<input name="password" type="password">
<br>
<input type="submit" value="Login">
</form>
</body>
</html>