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下创建一个上传图片的专属文件夹. 这个文件夹创建后,我们保存的图片就在该文件夹的真实路径下,但是在项目中是无法看到上传的图片的 ...
随机推荐
- windows2003通过iis配置ftp服务器
以前习惯于用filezilla作为windows的ftp服务器,但是现在新版本的filezilla已经不支持windows2003了,所以趁机试一下iis配置ftp服务器. 前面都是很常规的配置 参考 ...
- PHP中如何获取多个checkbox的值
> > > weeks后的中括号不可漏,否则用PHP获取的时候只能取到最后一个值.之后PHP就很好处理了,如下: PHP获取checkbox值方法一: $weeks = $_POST ...
- ios关于layer的一些常用属性
UILabel * labb = ... //set the border of labb labb.layer.borderWidth = 1; labb.layer.borderColor = [ ...
- 从一个新手容易混淆的例子简单分析C语言中函数调用过程
某天,王尼玛写了段C程序: #include <stdio.h> void input() { int i; ]; ; i < ; i++) { array[i] = i; } } ...
- 在windows 7搭建xcode开发环境
前言:本文适用于没有ios开发环境(如无iphone,mac等),在windows上搭建出ios的开发环境.值得注意的是,在windows搭建的ios开发环境只能做开发测试,无法发布产品.若需要发布产 ...
- Codeforces Round #359 (Div. 2) D. Kay and Snowflake 树的重心
题目链接: 题目 D. Kay and Snowflake time limit per test 3 seconds memory limit per test 256 megabytes inpu ...
- VMware虚拟机中Hadoop服务的端口无法访问的问题
今天安装了一个hadoop集群,因为已经在单个虚拟机上安装成功,所以初期安装相对顺利. 初始环境如下: 通过Vmware Esxi服务器虚拟机出来四台机器,每台机器的网络配置如下: ...
- Java多线程——<二>将任务交给线程,线程声明及启动
一.任务和线程 <thinking in java>中专门有一小节中对线程和任务两个概念进行了具体的区分,这也恰好说明任务和线程是有区别的. 正如前文所提到的,任务只是一段代码,一段要达成 ...
- texCUBE() to CubemapSampler.Sample()
update dx9 to dx11 refers to CUBEMAP sampler texCUBE(CubeMpaSampler,normal) maybe change to Cubema ...
- semantic
cgfx 里会有这个 float4X4 View : View; :后面这个 view 是一种 叫做user defined semantic provide the correct data to ...