Servlet学习--练习示例总结
醉了醉了。。本来想测试下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学习--练习示例总结的更多相关文章
- JSP&Servlet学习手册
JSP&Servlet学习手册 沙琪玛 书 目录 JSP 指令... 3 书写方式... 3 指令列表... 3 JSP 内置对象... 3 内置对象特点... 3 常用内置对象... 3 o ...
- activiti学习2:示例工程activiti-explorer.war的使用
目录 activiti学习2:示例工程activiti-explorer.war的使用 一.搭建开发环境 二.运行示例工程 三.示例工程功能演示 1. 创建流程图 2. 部署流程图 3. 启动流程 4 ...
- Servlet 学习笔记
Servlet 运行在服务器上的 java 类: Servlet 容器为 javaWeb 应用提供运行时环境,负责管理 servlet 和 jsp 生命周期,以及管理他们的共享数据. 现在我们知道了 ...
- ROS_Kinetic_29 kamtoa simulation学习与示例分析(一)
致谢源代码网址:https://github.com/Tutorgaming/kamtoa-simulation kamtoa simulation学习与示例分析(一) 源码学习与分析是学习ROS,包 ...
- Servlet学习:(三)Servlet3.0 上传文件
转: Servlet学习:(三)Servlet3.0 上传文件 2018年08月03日 11:57:58 iDark_CSDN 阅读数:362 一.注意事项 客户端(浏览器) 表单的提交方法必须是 ...
- bootstrap源码学习与示例:bootstrap-tab
http://www.cnblogs.com/rubylouvre/archive/2012/12/22/2829176.html bootstrap源码学习与示例 https://www.w3sc ...
- Servlet学习(九)——request
request运行流程在Servlet学习(四)——response已介绍,不再赘述 1.通过抓包工具获取Http请求 因为request代表请求,所以我们可以通过该对象分别获得Http请求的请求行, ...
- WebGPU学习(六):学习“rotatingCube”示例
大家好,本文学习Chrome->webgpu-samplers->rotatingCube示例. 上一篇博文: WebGPU学习(五): 现代图形API技术要点和WebGPU支持情况调研 ...
- # jsp及servlet学习笔记
目录 jsp及servlet学习笔记 JSP(Java Server Page Java服务端网页) 指令和动作: servlet(小服务程序) jsp及servlet学习笔记 JSP(Java Se ...
随机推荐
- 阻止文件不被上传到iCloud-b
有空用下 http://www.cocoachina.com/bbs/read.php?tid=86244 http://www.ooso.net/archives/617 http://blog.c ...
- 转:2014 年 15 款新评定的最佳 PHP 框架
原文来自于:http://blog.jobbole.com/59999/ 原文出处: codegeekz 译文出处:oschina 欢迎分享原创到伯乐头条 通常,框架都会被认为是帮助开发者快速 ...
- 【效率】FIND
文档 HTML Flash CSS 字体 命名颜色 工具 IMG
- 暴力求解——除法 Division,UVa 725
Description Write a program that finds and displays all pairs of 5-digit numbers that between them u ...
- [Sequence Alignment Methods] Dynamic time warping (DTW)
本系列介绍几种序列对齐方法,包括Dynamic time warping (DTW),Smith–Waterman algorithm,Cross-recurrence plot Dynamic ti ...
- DFS序 参考许昊然《数据结构漫谈》
网上特别讲DFS序的东西好像很少 太简单了? 实用性不大? 看了论文中 7个经典问题, 觉得挺有用的 原文 "所谓DFS序, 就是DFS整棵树依次访问到的结点组成的序列" &quo ...
- 《University Calculus》-chaper8-无穷序列和无穷级数-等比级数
前言:其实无穷序列和无穷级数和数列{an}以及我们接触微积分就给出的极限概念lim有着紧密的联系,它对于我们在具体的问题当中进行建模和数据分析有着非常重要的作用. 无穷序列: 最简单的一种说法,就是一 ...
- HTML的简单介绍
<html xmlns="http://www.w3.org/1999/xhtml"> <head><meta http-equiv="Co ...
- js中正则表达式的使用
1,作用:匹配一个字符串中的一些内容2,声明和使用: 1),构造函数 var reg=new RegExp(/表达式/) 2),字面量 var reg=/表达式/ 推荐使用 eg: var reg=/ ...
- sql server 常用小知识点
1. sql server的语法:中文要加 N select * from eVA_EMPBoard where name = N'施纪平' 而oracle的不用 2.