servlet forword服务器端跳转

Posted 小拽A

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了servlet forword服务器端跳转相关的知识,希望对你有一定的参考价值。

web.xml中配置servlet的映射和访问路径

<?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>HeadFirstJspServletChap05</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映射---->
<servlet>
<servlet-name>forwardServlet</servlet-name>
<servlet-class>com.java1234.web.ForwardServlet</servlet-class>
</servlet>
<!---servlet访问路径---->
<servlet-mapping>
<servlet-name>forwardServlet</servlet-name>
<url-pattern>/forward</url-pattern>
</servlet-mapping>
</web-app>

 

转发的servlet

package com.java1234.web;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class ForwardServlet extends HttpServlet{

/**
*
*/
private static final long serialVersionUID = 1L;

@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.doPost(request, response);
}

@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setAttribute("requestKey", "request值");
HttpSession session=request.getSession(); // 获取session
session.setAttribute("sessionKey", "session值");
ServletContext application=this.getServletContext(); // 获取application
application.setAttribute("applicationKey", "application值");
RequestDispatcher rd=request.getRequestDispatcher("target.jsp");
rd.forward(request, response); // 服务器调转/转发
}

}

<!---跳转的目标页面--->

target.jsp页面如下

<%@ 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=UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1>目标地址</h1>
request值:<%=request.getAttribute("requestKey") %><br/>
session值:<%=session.getAttribute("sessionKey") %><br/>
application值:<%=application.getAttribute("applicationKey") %><br/>
</body>
</html>

//终结:

1.redirect是客户端跳转,request不能设置参数,forward是服务器端跳转request能设置参数

 



























































以上是关于servlet forword服务器端跳转的主要内容,如果未能解决你的问题,请参考以下文章

Servlet总结

301 服务器端跳转与客户端跳转

服务器端跳转和客户端跳转

服务器端跳转与客户端跳转的区别

Servlet路径跳转问题

SpringMVC__配置视图解析以及服务端重定向和服务端跳转简单代码