第4章:作为Servlet:请求和响应/4.5 重定向和请求分派
其他
2018-11-04 14:11:30
阅读次数: 0
- 在浏览器端进行重定向:可以是HTML,也可以是JSP
- 代码:
package web;
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 Redirect extends HttpServlet {
/**
* Constructor of the object.
*/
public Redirect() {
super();
}
/**
* Destruction of the servlet. <br>
*/
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}
/**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
out.println("<HTML>");
out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>");
out.println(" <BODY>");
out.print(" This is ");
out.print(this.getClass());
out.println(", using the GET method");
out.println(" </BODY>");
out.println("</HTML>");
out.flush();
out.close();
}
/**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to post.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String sel = request.getParameter("sel");
if(sel.equals("1")){
//直接跳转到百度
response.sendRedirect("http://www.baidu.com");
}else if (sel.equals("2")){
//本应用的相对路径:
response.sendRedirect("form.html");
}else{ //其余的显示默认节目
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
out.println("<HTML>");
out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>");
out.println(" <BODY>");
out.print(" This is ");
out.print(this.getClass());
out.println(", using the POST method");
out.println(" </BODY>");
out.println("</HTML>");
out.flush();
out.close();
}
}
/**
* Initialization of the servlet. <br>
*
* @throws ServletException if an error occurs
*/
public void init() throws ServletException {
// Put your code here
}
}
中的doPost方法
- 测试:
- 使用绝对路径:
- 地址:http://localhost:8089/myWeb3/redirect.html
- 界面:
- 结果:
- 使用相对路径:
- 地址:http://localhost:8089/myWeb3/redirect.html
- 界面:
- 结果:
- 重定向注意事项
- 不能在out.println或者write之后调用sendRedirect,也就是输出界面或者write是二选一的
- sendRedirect方法的相对路径有两种形式
- 前面加“/”:表示从本应用的根目录开始,比如要跳转到本应用的form.html(位于myWeb3目录下),这种情况就要写:/myWeb3/form.html
- 前面不加"/",也就是默认当前请求URL地址的相对根目录的路径,当前地址的相对目录路径是http://localhost:8089/myWeb3/,所以重定位地址就是http://localhost:8089/myWeb3/redirect.html
- 概念:servlet将请求分发给应用的其它部分完成,比如jsp,这部分的工作是在服务端完成的,对客户端来说是透明的
- 源代码:
package web;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class Redirect extends HttpServlet {
/**
* Constructor of the object.
*/
public Redirect() {
super();
}
/**
* Destruction of the servlet. <br>
*/
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}
/**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
out.println("<HTML>");
out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>");
out.println(" <BODY>");
out.print(" This is ");
out.print(this.getClass());
out.println(", using the GET method");
out.println(" </BODY>");
out.println("</HTML>");
out.flush();
out.close();
}
/**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to post.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String sel = request.getParameter("sel");
if(sel.equals("1")){
//直接跳转到百度
response.sendRedirect("http://www.baidu.com");
}else if (sel.equals("2")){
//本应用的相对路径:
// response.sendRedirect("form.html");
response.sendRedirect("index.jsp");
}else if(sel.equals("3")){
//进行请求分派
RequestDispatcher requestDispatcher = request.getRequestDispatcher("form.html");
requestDispatcher.forward(request,response);
}
else{ //其余的显示默认节目
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
out.println("<HTML>");
out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>");
out.println(" <BODY>");
out.print(" This is ");
out.print(this.getClass());
out.println(", using the POST method");
out.println(" </BODY>");
out.println("</HTML>");
out.flush();
out.close();
}
}
/**
* Initialization of the servlet. <br>
*
* @throws ServletException if an error occurs
*/
public void init() throws ServletException {
// Put your code here
}
}
中的doPost方法
- 重定向和请求分派的区别
- 重定向在客户端进行,重定向后用户会看到他请求的URL变化的
- 请求分派在服务端进行的,这个对客户端是透明的,所以请求分派后客户端看到的URL是没有变化的
- 重定向可以重定向到其它应用(也就是外部网址都可以),但是请求分派只能是本应用的html或者JSP
- 重定向和请求分派的共同点是:输出(println或者write )和重定向(或者请求分派)只能二选一
转载自blog.csdn.net/u011830122/article/details/83714410