醉了醉了。。本来想测试下Servlet生命周期的,然后调了好久的错误,还是没成功,不知道为什么不能这样做

贴上代码:

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 LifeServlet extends HttpServlet{ private static final long serialVersionUID = 1L;
StringBuffer str;
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
str.append("处理Servlet请求-1");
doPost(request, response);
} @Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException { str.append("处理Servlet请求-2"); response.setCharacterEncoding("utf-8");
PrintWriter out;
out = response.getWriter();
out.println("<html>");
out.println("<head><title>fuck you</title></head>");
out.println(str);
out.println("</html>");
out.close();
} @Override
public void destroy() {
str.append("销毁Servlet");
} @Override
public void init() {
str.append("初始化Servlet");
} }

我只是想不在控制台打印出来而已,要不要这样啊

另外测试了下客户端跳转request请求是不行的,服务端跳转都可以

贴上客户端跳转 request,session,application三种请求代码示例

import java.io.IOException;

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 RedirectServlet 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.setAttribute("sessionKey", "sessionֵ");
ServletContext application=this.getServletContext();
application.setAttribute("applicationKey", "applicationֵ");
response.sendRedirect("target.jsp");
}
}

另外目标地址(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>

OK,下面贴上服务器跳转对应三种请求的代码示例:

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.setAttribute("sessionKey", "sessionֵ");
ServletContext application=this.getServletContext();
application.setAttribute("applicationKey", "applicationֵ");
RequestDispatcher rd=request.getRequestDispatcher("target.jsp");
rd.forward(request, response);
}
}

可以看出服务器端跳转只是

	RequestDispatcher rd=request.getRequestDispatcher("target.jsp");
rd.forward(request, response);

和客户端的不同,所以个人理解也就是客户端跳转应该是服务器通过response返回给客户端,然后客户端对应跳转url

另外:

贴个比较好的理解

1.forward跳转:

a.服务器端跳转,地址栏不改变;

b.执行到跳转语句后马上无条件跳转,之后的代码不再执行(跳转之前一定要释放全部资源);

c.request设置的属性在跳转后的页面仍可以使用;

d.使用<jsp:param name="参数名" value="参数值" />传递参数。



2.response跳转:

a.客户端跳转,地址栏改变;

b.所有代码执行完毕后跳转;

c.跳转后的页面不能使用上一个页面的request属性;

d.使用地址重写传递参数(response.sendRedirect("URL?参数名=参数值"))。

说实话里面的细节我也不懂,知道有这么回事就行了

Servlet学习--练习示例总结的更多相关文章

  1. JSP&Servlet学习手册

    JSP&Servlet学习手册 沙琪玛 书 目录 JSP 指令... 3 书写方式... 3 指令列表... 3 JSP 内置对象... 3 内置对象特点... 3 常用内置对象... 3 o ...

  2. activiti学习2:示例工程activiti-explorer.war的使用

    目录 activiti学习2:示例工程activiti-explorer.war的使用 一.搭建开发环境 二.运行示例工程 三.示例工程功能演示 1. 创建流程图 2. 部署流程图 3. 启动流程 4 ...

  3. Servlet 学习笔记

    Servlet 运行在服务器上的 java 类: Servlet 容器为 javaWeb 应用提供运行时环境,负责管理 servlet 和 jsp 生命周期,以及管理他们的共享数据. 现在我们知道了 ...

  4. ROS_Kinetic_29 kamtoa simulation学习与示例分析(一)

    致谢源代码网址:https://github.com/Tutorgaming/kamtoa-simulation kamtoa simulation学习与示例分析(一) 源码学习与分析是学习ROS,包 ...

  5. Servlet学习:(三)Servlet3.0 上传文件

    转: Servlet学习:(三)Servlet3.0 上传文件 2018年08月03日 11:57:58 iDark_CSDN 阅读数:362   一.注意事项 客户端(浏览器) 表单的提交方法必须是 ...

  6. bootstrap源码学习与示例:bootstrap-tab

    http://www.cnblogs.com/rubylouvre/archive/2012/12/22/2829176.html  bootstrap源码学习与示例 https://www.w3sc ...

  7. Servlet学习(九)——request

    request运行流程在Servlet学习(四)——response已介绍,不再赘述 1.通过抓包工具获取Http请求 因为request代表请求,所以我们可以通过该对象分别获得Http请求的请求行, ...

  8. WebGPU学习(六):学习“rotatingCube”示例

    大家好,本文学习Chrome->webgpu-samplers->rotatingCube示例. 上一篇博文: WebGPU学习(五): 现代图形API技术要点和WebGPU支持情况调研 ...

  9. # jsp及servlet学习笔记

    目录 jsp及servlet学习笔记 JSP(Java Server Page Java服务端网页) 指令和动作: servlet(小服务程序) jsp及servlet学习笔记 JSP(Java Se ...

随机推荐

  1. 学习opencv 第六章 习题十三

    用傅里叶变换加速卷积,直接上代码,Mat版是Copy他人的. CvMat版 #include "stdafx.h" #include "cv.h" #inclu ...

  2. Asp.net MVC分页实例

    分页是网页基本功能,这里主要讨论在Asp.net MVC环境下分页的前端实现,不涉及后台分页.实现效果如下图显示: Step 1.建立分页信息类 public class PagingInfo { p ...

  3. GO:格式化代码

    http://www.ituring.com.cn/article/39380 Go 开发团队不想要 Go 语言像许多其它语言那样总是在为代码风格而引发无休止的争论,浪费大量宝贵的开发时间,因此他们制 ...

  4. 转:PHP 5.4中的traits

    原文来自于:http://www.cnblogs.com/thinksasa/archive/2013/05/16/3081247.html PHP 5.4中的traits,是新引入的特性,中文还真不 ...

  5. BZOJ 1015 星球大战

    Description 很久以前,在一个遥远的星系,一个黑暗的帝国靠着它的超级武器统治者整个星系.某一天,凭着一个偶然的机遇,一支反抗军摧毁了帝国的超级武器,并攻下了星系中几乎所有的星球.这些星球通过 ...

  6. Kaggle Bike Sharing Demand Prediction – How I got in top 5 percentile of participants?

    Kaggle Bike Sharing Demand Prediction – How I got in top 5 percentile of participants? Introduction ...

  7. json数值和结构

    JSON 值可以是: l  数字(整数或浮点数) l  字符串(在双引号中) l  逻辑值(true 或 false) l  数组(在方括号中) l  对象(在花括号中) l  null JSON建构 ...

  8. android利用反射通过代码收缩通知栏

    最近有个需求,点击通知栏RemoteView中的按钮后要收起通知栏,系统默认是不自动收起的,不过没有找到公开的API可以控制通知栏. 在android.app.StatusBarManager里提供了 ...

  9. Gvim各种插件配置(windows环境下)

    1.Vundle插件:https://github.com/gmarik/Vundle.vim 用于管理Vim插件,安装此插件需要系统中已安装git,参考链接:Git for Windows安装和基本 ...

  10. (转载)PCNTL函数族--PHP多进程编程

    (转载)http://www.cnblogs.com/zox2011/archive/2013/02/19/2917448.html php有一组进程控制函数,使得php能在*nix系统中实现跟c一样 ...