1.客户端请求重定向跳转方式
2.服务器内部跳转方式
练习前提:
- 需要少量的servlet知识(out、response对象)
- 需要少量的html知识
- 需要少量的jsp知识
使用工具:myeclipse
打开myeclipse,新建web project ,new 一个servlet
以下是测试代码:
ServletPD.java
package sevlet;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
*author:恋晴
*http://blog.csdn.net/qq_32953185
*/
/**
* Servlet implementation class ServletPD
*/
@WebServlet("/ServletPD")
public class ServletPD extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public ServletPD() {
super();
// TODO Auto-generated constru- ctor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
//response.getWriter().append("Served at: ").append(request.getContextPath());
// response.setContentType("text/html;charset=utf-8");
// PrintWriter out=response.getWriter();
// out.println("<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">");
// out.println("<html>");
// out.println("<head><title>servlet!!!</title></head>");
// out.println("<body>");
// out.println("<h1>你好,我是servlet!<h1><br>");
// out.println("</body>");
// out.println("</html");
// out.flush();
// out.close();
doPost(request,response);
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
//doGet(request, response);
//1.请求重定向方式跳转
response.sendRedirect(request.getContextPath()+"/test.jsp");
//2.服务器内部跳转
//request.getRequestDispatcher("test.jsp").forward(request,response);
}
}
index.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>servlet路径跳转test~</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>
<a href="/ServletPathDirection/ServletPD">访问ServletPD,跳转到test.jsp</a><br>
</body>
</html>
test.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>跳转页面</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>
<h1>这里是test.jsp页面~</h1><br>
</body>
</html>