您的位置:首页 > 产品设计 > UI/UE

req.getRequestDispatcher和resp.sendRedirect的区别

2016-04-26 09:48 447 查看
1、index.jsp到main.jsp:

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>

<%

String path = request.getContextPath();

String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";

%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>

  <head>

    <base href="<%=basePath%>">

    

    <title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">    
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->

  </head>

  

  <body>

  

    <form action="loginServlet" method="post">

     <div>

            文本框:<input type="text" name="text1" id="text1"/><br/>

            

            密码框:<input type="password" name="password" id="password"/><br/>

     

             选择您居住城市:

             <select name="select1" id="select1" >
            <option value="上海">上海</option>
            <option value="北京">北京</option>
            <option value="天津">天津</option>
            <option value="广东">广东</option>

             </select>    

     <br/>      

             您喜欢的运动(checkbox):

     <br><input type="checkbox" name="checkbox1" value="打篮球"/> 打篮球<br/>    

     <input type="checkbox" name="checkbox1" value="踢足球"/>踢足球 <br/>   

     <input type="checkbox" name="checkbox1" value="瑜伽"/>瑜伽 <br/>   

     <input type="checkbox" name="checkbox1" value="游泳"/>游泳 <br/> 

     

             您的性别:(radio)

     <input type="radio" name="radio1" value="女"/>女

     <input type="radio" name="radio1" value="男"/>男<br/> 

            请选择文件:

        <input type="file" name="file1"/>  <br/>   

      

            隐藏域: <input type="hidden" name="hid" value="我是隐藏域"/> <br/>  

           文本域:<textarea rows="5" cols="4" name="area" value="文本域">文本域</textarea> <br/>       

            

     <input type="submit" value="提交"/>

     <input type="reset" value="重置"/>

     </div>

    </form>

    

    

  </body>

</html>

2、loginServlet 处理index.jsp请求

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;

import sun.security.util.Password;

public class loginServlet extends HttpServlet{
public loginServlet()
{
super();
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {

  resp.setContentType("text/html;charset=GB2312");
   req.setCharacterEncoding("utf-8");
PrintWriter out=resp.getWriter();

String text=req.getParameter("text1");
String pas=req.getParameter("password");
String selected=req.getParameter("select1");
String checkselect []=req.getParameterValues("checkbox1");
String radioselect=req.getParameter("radio1");
String file1=req.getParameter("file1");
String area=req.getParameter("area");

out.print("文本框:"+text+"<br/>");
out.print("密码框"+pas+"<br/>");
out.print("select:"+selected+"<br/>");
out.print("checkbox:"+checkselect+"<br/>");

out.print("--------------------------"+"<br/>");
for(String x : checkselect)
{
out.print("增强for循环checkbox:"+x+"<br/>");
}

for(int i=0;i<checkselect.length;++i)
out.print("checkbox:"+checkselect[i]+"<br/>");
out.print("--------------------------"+"<br/>");
out.print("radio:"+radioselect+"<br/>");

out.print("file:"+file1+"<br/>");
out.print("area:"+area+"<br/>");

req.setAttribute("text1", text);
req.getSession().setAttribute("pas", pas);

//req.getRequestDispatcher("main.jsp").forward(req, resp);
resp.sendRedirect("main.jsp");

}
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
// TODO Auto-generated method stub
super.doGet(req, resp);
}
@Override
public void destroy() {
// TODO Auto-generated method stub
super.destroy();
}
@Override
public void init() throws ServletException {
// TODO Auto-generated method stub
super.init();
}
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
// TODO Auto-generated method stub
super.service(req, resp);
}

}

3、index.jsp 跳转的main.jsp

<%@ page language="java" contentType="text/html; charset=utf-8"

    pageEncoding="utf-8"%>

   <%

   out.print("request:"+request.getAttribute("text1"));

   

   out.print("session:"+  session.getAttribute("pas"));

   

    %> 

    

    

<!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>

</body>

</html>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: