package gacl.servlet.study;
import java.io.IOException;
import java.util.Enumeration;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class ServletConfigDemo1 extends HttpServlet {
/**
* 定义ServletConfig对象来接收配置的初始化参数
*/
private ServletConfig config;
/**
* 当servlet配置了初始化参数后,web容器在创建servlet实例对象时,
* 会自动将这些初始化参数封装到ServletConfig对象中,并在调用servlet的init方法时,
* 将ServletConfig对象传递给servlet。进而,程序员通过ServletConfig对象就可以
* 得到当前servlet的初始化参数信息。
*/
@Override
public void init(ServletConfig config) throws ServletException {
this.config = config;
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//获取在web.xml中配置的初始化参数
String paramVal = this.config.getInitParameter("name");//获取指定的初始化参数
response.getWriter().print(paramVal);
response.getWriter().print("<hr/>");
//获取所有的初始化参数
Enumeration<String> e = config.getInitParameterNames();
while(e.hasMoreElements()){
String name = e.nextElement();
String value = config.getInitParameter(name);
response.getWriter().print(name + "=" + value + "<br/>");
}
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.doGet(request, response);
}
}
1 : Sevlert 概念
是sun 公司提供一门用于开发动态web资源的技术
步骤;
1:编写java 类, 实现servlet 接口
2: 把开发java 类部署到 服务器
2: Sevlert 的运行过程:
a web 服务器首先检查是否已经装载并创建了该servlet 的实例对象, 如果是 则跳到d
b 创建了一个servlert 的一个实例
c 调用了servlert 的 init( ) 方法
d 创建一个用于封装http 请求消息的HttpServlertRequest 的一个对象, 一个代表响应消息的HttpServlertResponse 对象, 然后调用
servlet 的 service 的方法, 并将请求和响应参数传递回去。
e web 在重启或者停止之前, servlet 引擎将卸载Servlet ,在卸载之前会调用 destroy()方法。
Servlet 调用图:
4: 在 Eclipse 中开发Servlet
创建的步骤:
a :
b :
c:
生成的代码如果所示:
package gacl.servlet.study;
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 ServletDemo1 extends HttpServlet {
/**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
out.println("<HTML>");
out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>");
out.println(" <BODY>");
out.print(" This is ");
out.print(this.getClass());
out.println(", using the GET method");
out.println(" </BODY>");
out.println("</HTML>");
out.flush();
out.close();
}
/**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to post.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
out.println("<HTML>");
out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>");
out.println(" <BODY>");
out.print(" This is ");
out.print(this.getClass());
out.println(", using the POST method");
out.println(" </BODY>");
out.println("</HTML>");
out.flush();
out.close();
}
}
自然而然 web.xml 里也会增加两个元素, 如图所示:
5:
5.1 servlet 开发注意的细节:
由于客户端是通过url 地址访问web 服务器中的资源, 所以servlet 程序若想被外界访问, 必须把servlet 映射到一个 url 的地址上,
这个工作在 web.xml
文件中使用:
<servlet>和 <servlet-mapping >
<servlet> 里包括两个子元素
<servlet-name> 代表注册名称
<servlet-class> 代表的是完整的类名
<sevlet-mapping>
<servlet-name> 代表注册名称
<servlet-mapping> 代表对外访问的地址
5.2 url 的通配符的映射
两种固定的格式:
一种是“ *. 扩展名” , 还有一种是 “/” 开头以“/*” 结尾
5.3 Servlet 和java 类的区别
Servlet 是一个有servlet 引擎调用的java 类, 它不能独立运行。
针对客户端的多次Servlet 请求, 通常情况下, 服务器只会创建一个servlet 实例对象, 也就是说Servlet 实例对象一旦被创建,
他就会驻留在内存中, 为后续的其他请求服务,直到web容器退出。
在servlet 的整个生命周期内, init()方t只会被调用一次, 而对一个servlet 的每次访问请求都会导致servlce方法的调用,
对于每次访问请求, servlet 引擎都会被创建一个新的请求和响应对象, 然后将这两个参数作为对象传入sevice 方法里。
如果在<servlet>元素中配置了一个<load-on-startup>元素,那么WEB应用程序在启动时,就会装载并创建Servlet的实例对象、以及调用Servlet实例对象的init()方法。
举例:
<servlet>
<servlet-name>invoker</servlet-name>
<servlet-class>
org.apache.catalina.servlets.InvokerServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
5.4 缺省的servlet
如果某个Servlet的映射路径仅仅为一个正斜杠(/),那么这个Servlet就成为当前Web应用程序的缺省Servlet。
凡是在web.xml文件中找不到匹配的<servlet-mapping>元素的URL,它们的访问请求都将交给缺省Servlet处理,也就是说,缺省Servlet用于处理所有其他Servlet都不处理的访问请求
5.5 Servlet 的线程安全
看到的一个最典型的标记接口就是"Serializable",这个接口也是没有定义任何方法和常量的,标记接口在Java中有什么用呢?主要作用就是给某个对象打上一个标志,告诉JVM,这个对象可以做什么,比如实现了"Serializable"接口的类的对象就可以被序列化
6 : servletConfig
6.1 配置初始化参数
<servlet>
2 <servlet-name>ServletConfigDemo1</servlet-name>
3 <servlet-class>gacl.servlet.study.ServletConfigDemo1</servlet-class>
4 <!--配置ServletConfigDemo1的初始化参数 -->
5 <init-param>
6 <param-name>name</param-name>
7 <param-value>gacl</param-value>
8 </init-param>
9 <init-param>
10 <param-name>password</param-name>
11 <param-value>123</param-value>
12 </init-param>
13 <init-param>
14 <param-name>charset</param-name>
15 <param-value>UTF-8</param-value>
16 </init-param>
17 </servlet>
6.2 获取初始化参数
当servlet 配置了初始化参数的时候, web容器在创建servlet 实例对象时,会自动门的将这些初始化的参数赋值给ServletConfig对象, 然后在servlet 初始化的时候将 servletConfig 作为一个参数传递给了servlet 方法
package gacl.servlet.study;
import java.io.IOException;
import java.util.Enumeration;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class ServletConfigDemo1 extends HttpServlet {
/**
* 定义ServletConfig对象来接收配置的初始化参数
*/
private ServletConfig config;
/**
* 当servlet配置了初始化参数后,web容器在创建servlet实例对象时,
* 会自动将这些初始化参数封装到ServletConfig对象中,并在调用servlet的init方法时,
* 将ServletConfig对象传递给servlet。进而,程序员通过ServletConfig对象就可以
* 得到当前servlet的初始化参数信息。
*/
@Override
public void init(ServletConfig config) throws ServletException {
this.config = config;
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//获取在web.xml中配置的初始化参数
String paramVal = this.config.getInitParameter("name");//获取指定的初始化参数
response.getWriter().print(paramVal);
response.getWriter().print("<hr/>");
//获取所有的初始化参数
Enumeration<String> e = config.getInitParameterNames();
while(e.hasMoreElements()){
String name = e.nextElement();
String value = config.getInitParameter(name);
response.getWriter().print(name + "=" + value + "<br/>");
}
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.doGet(request, response);
}
}
7: sevletContext
web容器中, 他会为每个web应用程序都创建一个对应的ServletContext 对象, 它代表整个web应用,
servletConfig 对象维护了ServletContext对象的引用, 可以通过ServletConfig,getServletContext 方法获得ServletContext 对象
由于web应用中所有的servlet 共享一个servletContext 对象, 因此通富哦servletContext 对象来实现通讯
7.1 数据共享
package gacl.servlet.study;
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;
public class ServletContextDemo1 extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String data = "xdp_gacl";
/**
* ServletConfig对象中维护了ServletContext对象的引用,开发人员在编写servlet时,
* 可以通过ServletConfig.getServletContext方法获得ServletContext对象。
*/
ServletContext context = this.getServletConfig().getServletContext();//获得ServletContext对象
context.setAttribute("data", data); //将data存储到ServletContext对象中
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
package gacl.servlet.study;
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;
public class ServletContextDemo2 extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
ServletContext context = this.getServletContext();
String data = (String) context.getAttribute("data");//从ServletContext对象中取出数据
response.getWriter().print("data="+data);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" 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">
<display-name></display-name>
<!-- 配置WEB应用的初始化参数 -->
<context-param>
<param-name>url</param-name>
<param-value>jdbc:mysql://localhost:3306/test</param-value>
</context-param>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
获取参数:
package gacl.servlet.study;
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;
public class ServletContextDemo3 extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
ServletContext context = this.getServletContext();
//获取整个web站点的初始化参数
String contextInitParam = context.getInitParameter("url");
response.getWriter().print(contextInitParam);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
7.3 用servletContext实现转发
package gacl.servlet.study;
2
3 import java.io.IOException;
4 import java.io.PrintWriter;
5 import javax.servlet.RequestDispatcher;
6 import javax.servlet.ServletContext;
7 import javax.servlet.ServletException;
8 import javax.servlet.http.HttpServlet;
9 import javax.servlet.http.HttpServletRequest;
10 import javax.servlet.http.HttpServletResponse;
11
12 public class ServletContextDemo4 extends HttpServlet {
13
14 public void doGet(HttpServletRequest request, HttpServletResponse response)
15 throws ServletException, IOException {
16 String data = "<h1><font color='red'>abcdefghjkl</font></h1>";
17 response.getOutputStream().write(data.getBytes());
18 ServletContext context = this.getServletContext();//获取ServletContext对象
19 RequestDispatcher rd = context.getRequestDispatcher("/servlet/ServletContextDemo5");//获取请求转发对象(RequestDispatcher)
20 rd.forward(request, response);//调用forward方法实现请求转发
21 }
22
23 public void doPost(HttpServletRequest request, HttpServletResponse response)
24 throws ServletException, IOException {
25 }
26 }
package gacl.servlet.study;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class ServletContextDemo5 extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.getOutputStream().write("servletDemo5".getBytes());
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.doGet(request, response);
}
}
转自 :http://www.cnblogs.com/xdp-gacl/p/3760336.html
感谢博主