Servlet的应用
1、重定向
package com.yyq.sendredirect;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
/**
* Created by gao on 16-4-14.
*/
public class Servlet01 extends HttpServlet{
//处理HTTP GET请求
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doPost(req, resp);
}
//处理HTTP POST请求
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//重新定向到servlet02
resp.sendRedirect("s02");
}
@Override
public void destroy() {
//空
}
@Override
public void init() throws ServletException {
//空
}
}
Servlet02类:
package com.yyq.sendredirect;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
/**
* Created by gao on 16-4-14.
*/
public class Servlet02 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doPost(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.setContentType("text/html;charset=utf-8");
PrintWriter out = resp.getWriter();
out.println("<html>");
out.println("<head><title>Servlet06</head></title>");
out.println("<body>");
out.print("在Servlet02中获取请求参数name的值");
out.print(req.getParameter("name"));
out.println("</body>");
out.println("</html>");
}
@Override
public void destroy() {
//空
}
@Override
public void init() throws ServletException {
//空
}
}
web.xml注册:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0"> <servlet>
<servlet-name>servlet01</servlet-name>
<servlet-class>com.yyq.sendredirect.Servlet01</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>servlet01</servlet-name>
<url-pattern>/s01</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>servlet02</servlet-name>
<servlet-class>com.yyq.sendredirect.Servlet02</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>servlet02</servlet-name>
<url-pattern>/s02</url-pattern>
</servlet-mapping>
</web-app>
启动运行:http://localhost:8080/s01?name=test
package com.yyq.requestdispatcher;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
/**
* Created by gao on 16-4-14.
*/
public class Servlet03 extends HttpServlet {
//处理HTTP GET请求
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doPost(req, resp);
}
//处理HTTP POST请求
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//获取请求分派器
RequestDispatcher dispatcher = req.getRequestDispatcher("servlet04");
//将请求转发至指定路径的资源
dispatcher.forward(req,resp);
}
@Override
public void destroy() {
//空
}
@Override
public void init() throws ServletException {
//空
}
}
Servlet04类:
package com.yyq.requestdispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
/**
* Created by gao on 16-4-14.
*/
public class Servlet04 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doPost(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.setContentType("text/html;charset=utf-8");
PrintWriter out = resp.getWriter();
out.println("<html>");
out.println("<head><title>Servlet06</head></title>");
out.println("<body>");
out.print("在Servlet03中获取请求参数name的值");
out.print(req.getParameter("name"));
out.println("</body>");
out.println("</html>");
}
@Override
public void destroy() {
//空
}
@Override
public void init() throws ServletException {
//空
}
}
web.xml注册:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0">
<servlet>
<servlet-name>servlet03</servlet-name>
<servlet-class>com.yyq.requestdispatcher.Servlet03</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>servlet03</servlet-name>
<url-pattern>/servlet03</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>servlet04</servlet-name>
<servlet-class>com.yyq.requestdispatcher.Servlet04</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>servlet04</servlet-name>
<url-pattern>/servlet04</url-pattern>
</servlet-mapping>
</web-app>
启动Tomcat,输入:http://localhost:8080/servlet03?name=test
- setAttribute(String name,Object o)方法:将数据作为request对象的一个属性存放到request对象中,例如:request.setAttribute("data", data);
- getAttribute(String name)方法:获取request对象的name属性的属性值,例如:request.getAttribute("data")
- removeAttribute(String name)方法:移除request对象的name属性,例如:request.removeAttribute("data")
- Enumeration getAttributeNames()方法,获取request对象的所有属性名,返回的是一个Enumeration 对象,例如:Enumeration<String> attrNames = request.getAttributeNames();
这种存储在HttpServlet中的对象称为请求域属性,属于同一请求过程的多个处理模块之间可以通过请求域属性来传递对象数据,如通过请求转发的两个Servlet之间就可以通过请求域属性来传递对象数据,但通过重定向的两个Servlet之间就不能通过请求域来传递对象数据。Servlet05类:
package com.yyq.attributerequest;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
/**
* Created by gao on 16-4-14.
*/
public class Servlet05 extends HttpServlet {
//处理HTTP GET请求
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doPost(req, resp);
}
//处理HTTP POST请求
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String str = "在Servlet05中存放请求域属性";
req.setAttribute("content",str);
//获取请求分派器
RequestDispatcher dispatcher = req.getRequestDispatcher("servlet06");
//将请求转发至指定路径的资源
dispatcher.forward(req,resp);
}
@Override
public void destroy() {
//空
}
@Override
public void init() throws ServletException {
//空
}
}Servlet06类:
package com.yyq.attributerequest;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
/**
* Created by gao on 16-4-14.
*/
public class Servlet06 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doPost(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.setContentType("text/html;charset=utf-8");
PrintWriter out = resp.getWriter();
out.println("<html>");
out.println("<head><title>Servlet06</head></title>");
out.println("<body>");
String str = (String) req.getAttribute("content");
out.print("从Servlet05中获取请求域属性content的值为:" + str);
out.println("</body>");
out.println("</html>");
}
@Override
public void destroy() {
//空
}
@Override
public void init() throws ServletException {
//空
}
}web.xml注册:<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0">
<servlet>
<servlet-name>servlet05</servlet-name>
<servlet-class>com.yyq.attributerequest.Servlet05</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>servlet05</servlet-name>
<url-pattern>/servlet05</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>servlet06</servlet-name>
<servlet-class>com.yyq.attributerequest.Servlet06</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>servlet06</servlet-name>
<url-pattern>/servlet06</url-pattern>
</servlet-mapping>
</web-app>启动Tomcat,输入:http://localhost:8080/servlet05
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0">
<servlet>
<servlet-name>initparamservlet</servlet-name>
<servlet-class>com.yyq.initparam.InitParamServlet</servlet-class>
<!--定义一个Servlet初始化参数-->
<init-param>
<!--参数名-->
<param-name>encoding</param-name>
<!--参数值-->
<param-value>utf-8</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>initparamservlet</servlet-name>
<url-pattern>/initparamservlet</url-pattern>
</servlet-mapping>
</web-app>
package com.yyq.initparam;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
/**
* Created by gao on 16-4-14.
*/
public class InitParamServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
this.doPost(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//获取ServletConfig实例
ServletConfig config = this.getServletConfig();
//获取指定名称的初始化参数的字符串值
String str = config.getInitParameter("encoding");
resp.setContentType("text/html;charset=utf-8");
PrintWriter out = resp.getWriter();
out.println("<html>");
out.println("<head><title>initparamservlet</head></title>");
out.println("<body>");
out.print("获取InitParamServlet的初始化参数\"encoding\"的字符串值为:" + str);
out.println("</body>");
out.println("</html>");
}
@Override
public void destroy() {
super.destroy();
}
@Override
public void init() throws ServletException {
super.init();
}
}
Servlet的初始化参数只是针对当前这个Servlet类有效,在本Servlet类中只能获取自身的初始化参数,无法获取其他Servlet类的初始化参数。
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0">
<servlet>
<servlet-name>servletcontextservlet</servlet-name>
<servlet-class>com.yyq.servletcontext.ServletContextServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>servletcontextservlet</servlet-name>
<url-pattern>/servletcontextservlet</url-pattern>
</servlet-mapping>
<!--定义一个Web应用上下文初始化参数-->
<context-param>
<!--参数名-->
<param-name>appName</param-name>
<!--参数值-->
<param-value>Servlet入门基础</param-value>
</context-param>
</web-app>
b、在Servlet类中获取Web应用上下文的初始化参数值
package com.yyq.servletcontext;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
/**
* Created by gao on 16-4-14.
*/
public class ServletContextServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
this.doPost(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//获取ServletContext实例
ServletContext context = this.getServletContext();
//获取指定名称的web应用上下文初始参数的字符串值
String str = context.getInitParameter("appName");
resp.setContentType("text/html;charset=utf-8");
PrintWriter out = resp.getWriter();
out.println("<html>");
out.println("<head><title>servletcontextservlet</head></title>");
out.println("<body>");
out.print("获取ServletContext的初始化参数\"appName\"的字符串值为:" + str);
out.println("</body>");
out.println("</html>");
}
@Override
public void destroy() {
super.destroy();
}
@Override
public void init() throws ServletException {
super.init();
}
}
c、启动Tomcat
Servlet的应用的更多相关文章
- servlet文件下载
创建web工程servlet,新建DownloadServlet.java package com.xmyself.servlet; import java.io.File; import java. ...
- java中servlet的各种路径
1. web.xml中<url-pattern>路径,(叫它Servlet路径!) > 要么以“*”开关,要么为“/”开头 2. 转发和包含路径 > *****以“/”开头:相 ...
- Servlet监听器笔记总结
监听器Listener的概念 监听器的概念很好理解,顾名思义,就是监视目标动作或状态的变化,目标一旦状态发生变化或者有动作,则立马做出反应. Servlet中的也有实现监听器的机制,就是Listene ...
- JavaWeb——Servlet
一.基本概念 Servlet是运行在Web服务器上的小程序,通过http协议和客户端进行交互. 这里的客户端一般为浏览器,发送http请求(request)给服务器(如Tomcat).服务器接收到请求 ...
- servlet 简介,待完善
什么是Servlet?① Servlet就是JAVA 类② Servlet是一个继承HttpServlet类的类③ 这个在服务器端运行,用以处理客户端的请求 Servlet相关包的介绍--javax. ...
- java web学习总结(五) -------------------servlet开发(一)
一.Servlet简介 Servlet是sun公司提供的一门用于开发动态web资源的技术. Sun公司在其API中提供了一个servlet接口,用户若想用发一个动态web资源(即开发一个Java程序向 ...
- servlet使用入门
创建web工程servlet,然后新建TestServlet.java package com.xmyself.servlet; import java.io.IOException; import ...
- 基于jsp+servlet图书管理系统之后台万能模板
前奏: 刚开始接触博客园写博客,就是写写平时学的基础知识,慢慢发现大神写的博客思路很清晰,知识很丰富,非常又价值,反思自己写的,顿时感觉非常low,有相当长一段时间没有分享自己的知识.于是静下心来钻研 ...
- [Servlet] 初识Servlet
什么是Servlet? 定义 Servlet的全称是 Server Applet,顾名思义,就是用 Java 编写的服务器端程序. Servlet 是一个 Java Web开发标准,狭义的Servle ...
- Java Servlet+Objective-c图上传 步骤详细
一. Servlet 1.创建图片保存的路径 在项目的WebContent下创建一个上传图片的专属文件夹. 这个文件夹创建后,我们保存的图片就在该文件夹的真实路径下,但是在项目中是无法看到上传的图片的 ...
随机推荐
- Spark 大数据平台 Introduction part 2 coding
Basic Functions sc.parallelize(List(1,2,3,4,5,6)).map(_ * 2).filter(_ > 5).collect() *** res: Arr ...
- django post分号引发的问题
利用jquery的ajax传值 $.ajax({ type:"POST", url:"", data:"content"=content, ...
- ORA-00845
系统版本: [root@yoon ~]# more /etc/oracle-releaseOracle Linux Server release 5.7 数据库版本: Oracle Database ...
- 0x03伪指令
等号伪指令 = 相当于指定常量,由等号定义的符号常量不占用存储空间. count = 1234 可以重复定义多次,EQU则不容许 EQU伪指令 1.常量名 EQU 表达式 NUMBER EQU 10* ...
- iOS 系统二维码扫描(可限制扫描区域)
使用 AVFoundation系统库来进行二维码扫描并且限制扫描二维码的范围.(因为默认的是全屏扫描) -(void)beginCode { //1.摄像头设备 AVCaptureDevice *de ...
- Surrounded Regions
Surrounded Regions Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'. A ...
- javascript之流程控制 和函数的容易忽略点
1.流程控制 1> for in 仅用于 对象的遍历: var box={ "name":'小红', 'age':18, 'height':165 }; for(var b ...
- LintCode-Word Segmentation
Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separa ...
- Java 8 VM GC Tuning Guide Charter3-4
第三章 Generations One strength of the Java SE platform is that it shields the developer from the compl ...
- Useful related java API for Android
Language_suport and Other Language-Oriented API: strings,exceptions, threads, #java.lang.* offers th ...