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.java.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.java.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服务器端跳转的更多相关文章

  1. servlet运行机制、Request内置对象和服务器端跳转

    servlet运行机制: 当发送一个请求到服务器的时候,容器(Tomcat)会判断该路径属于哪一个 Servlet 进行处理,Servlet 有一个抽象父类“HttpServlet”,这个类是一个模板 ...

  2. Java开发之Servlet之间的跳转

    一.转向(Forward) 1.要点说明 转向是通过RequestDispatcher对象的forward()方法来实现的.RequestDispatcher可以通过HttpServletReques ...

  3. Servlet间的跳转

       Forward        转向(Forward)是通过RequestDispatcher对象的forward(HTTPServletRequest req, HttpSerletRespon ...

  4. Jsp与servlet之间页面跳转及参数传递实例(转)

    原网址:http://blog.csdn.net/ssy_shandong/article/details/9328985 11. jsp与servlet之间页面跳转及参数传递实例 分类: Java ...

  5. JSP中客户端跳转与服务器端跳转的区别

    转载自:https://www.cnblogs.com/memewry/archive/2012/08/21/2649988.html 客户端跳转时用HttPservletResopse对象的send ...

  6. Servlet——提交表单信息,Servlet之间的跳转

    HTML表单标签:<form></form> 属性: actoion:  提交到的地址,默认为当前页面 method:  表单提交方式 有get和post两种方式,默认为get ...

  7. SpringMvc的服务器端跳转和客户端跳转

    首先,找到 package org.springframework.web.servlet.view; public class InternalResourceViewResolver extend ...

  8. servlet篇 之 跳转问题

    servlet中的跳转: 跳转本质上是对文件传输操作 进行 封装. 例如:客户端访问一个servlet,在servlet中需要把已经写好的页面hello.html返回给客户端浏览器,需要用io来实现这 ...

  9. Servlet(11)—客户端跳转和服务端跳转

    客户端跳转: 1.链接跳转:< a href="">< /a > 2.表单提交< form>< /form> 3.Response. ...

随机推荐

  1. VR虚拟现实眼镜那些事

    今天是2014.3.20,笔者从oculus官网订了DK2(第二代开发版) 评测视频http://v.youku.com/v_show/id_XNjg3NTUzOTk2.html 想想从哪说起呢... ...

  2. poj3535 A+B (大数加法)

    A+B Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 811   Accepted: 371 Description The ...

  3. <c和指针>学习笔记3之操作符,表达式与指针

    1 操作符 (1)移位操作符 左移<<:值最左边的几位丢弃,右边多出来的几个空位用0补齐 01101101 011(丢弃)01101000(后面三位补0) 右移>>: 算术左移 ...

  4. C# 写 LeetCode easy #13 Roman to Integer

    13.Roman to Integer Roman numerals are represented by seven different symbols: I, V, X, L, C, D and  ...

  5. POJ2549【hash分离链接法】

    题意: 给n个不同的数,求一个4个数(a,b,c,d)的组合满足a+b+c=d;求最大的d. 思路: 没想到可以用hash搞/ 这个就是数据结构里的分离链接法~ 解决hash冲突的方法:将所有关键字为 ...

  6. 3DMAX 10 角色动作

    基本流程 1保存初始姿势(保存原始T动作) 2确定动画帧数时间 3找参考动作姿态,绘制关键帧草图 4先调整出初始姿势,如果是循环动画,需要把第一帧复制到最后一帧 5大体先想好在固定时间比例调草图的关键 ...

  7. anaconda3安装caffe

    使用anaconda3安装caffe踩坑无数次,放弃治疗,直接在~/.bashrc中删除anaconda的路径,备份一下等要用的时候再写上,用默认的python2.7系统环境安装 要使用人脸检测项目中 ...

  8. SpringMVC重定向传递参数

    在SpringMVC的一个controller中要把参数传到页面,只要配置视图解析器,把参数添加到Model中,在页面用el表达式就可以取到.但是,这样使用的是forward方式,浏览器的地址栏是不变 ...

  9. IBM WebSphere MQ

    相关链接: http://kakajw.iteye.com/category/269774 http://www.ibm.com/support/knowledgecenter/zh/SSFKSJ_7 ...

  10. ES6新特性使用小结(四)

    十一.Proxy .Reflect ①.Proxy 的概念和常用方法 { let obj = { //1.定义原始数据对象 对用户不可见 time: '2017-09-20', name: 'net' ...