3.2 A Servlet That
Generates Plain Text
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class HelloWorld extends HttpServlet {
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
PrintWriter out = response.getWriter();
out.println("Hello World");
}
}
3.3 A Servlet That Generates HTML
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
/** Simple servlet used to test server. */
public class HelloServlet extends HttpServlet {
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
String docType =
"<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 " +
"Transitional//EN\">\n";
out.println(docType +
"<HTML>\n" +
"<HEAD><TITLE>Hello</TITLE></HEAD>\n" +
"<BODY BGCOLOR=\"#FDF5E6\">\n" +
"<H1>Hello</H1>\n" +
"</BODY></HTML>");
}
}
web的中间作用
doGet() and doPost()都接受两个参数: HttpServletRequest和HttpServletResponse.
相应的包:java.io(PrintWriter), javax.servlet(HttpServlet),javax.servlet.http(HttpServletRequest and HttpServletResponse)
一般很少使用servlet生成格式相对固定的HTML页面(即每次请求,页面的布局改动很小);这种情况下JSP常常更为方便。
a variation of the HelloServlet class that makes use of this utility
package coreservlets;
import javax.servlet.*;
import javax.servlet.http.*;
/** Some simple time savers. Note that most are static methods. */
public class ServletUtilities {
public static final String DOCTYPE =
"<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 " +
"Transitional//EN\">";
public static String headWithTitle(String title) {
return(DOCTYPE + "\n" +
"<HTML>\n" +
"<HEAD><TITLE>" + title + "</TITLE></HEAD>\n");
}
...
}
and
package coreservlets;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
/** Simple servlet for testing the use of packages
* and utilities from the same package.
*/
public class HelloServlet3 extends HttpServlet {
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
String title = "Hello (3)";
out.println(ServletUtilities.headWithTitle(title) +
"<BODY BGCOLOR=\"#FDF5E6\">\n" +
"<H1>" + title + "</H1>\n" +
"</BODY></HTML>");
}
}
3.6 The Servlet Life Cycle
3.6.1 The service Method
服务器每次接收到对servlet的请求,都会产生一个新的线程,调用service方法。sevice检查HTTP请求的类型(GET,POST,PUT,DELETE等)并相应的调用doGet,doPost,doPut,doDelete等方法。
如果需要在servlet中等同地处理POST和GET请求,只需让doPost调用doGet(或相反)即可。
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
// Servlet code
}
public void doPost(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
写道
If your servlet needs to handle both GET and POST identically, have your
doPost method call doGet, or vice versa. Don’t override service.
3.6.2 The doGet, doPost, and doXxx Methods
Servlet的主体。
Ninety-nine percent of the
time, you only care about GET or POST requests, so you override doGet and/or
doPost. However, if you want to, you can also override doDelete for DELETE
requests, doPut for PUT, doOptions for OPTIONS, and doTrace for TRACE.
3.6.3 init
有时希望在servlet首次载入时,执行复杂的初始化任务,但并不想每个请求都重复这些任务。init方法就专为这种情况设计,它在servlet初次创建时被调用,之后处理每个用户的请求是则不再调用这个方法。
public void init() throws ServletException {
// Initialization code...
}
写道
The init method performs two varieties of initializations: general initializations
and initializations controlled by initialization parameters.
General Initializations
package coreservlets;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
/** Example using servlet initialization and the
* getLastModified method.
*/
public class LotteryNumbers extends HttpServlet {
private long modTime;
private int[] numbers = new int[10];
/** The init method is called only when the servlet is first
* loaded, before the first request is processed.
*/
public void init() throws ServletException {
// Round to nearest second (i.e., 1000 milliseconds)
modTime = System.currentTimeMillis()/1000*1000;
for(int i=0; i<numbers.length; i++) {
numbers[i] = randomNum();
}
}
/** Return the list of numbers that init computed. */
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
String title = "Your Lottery Numbers";
String docType =
"<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 " +
"Transitional//EN\">\n";
out.println(docType +
"<HTML>\n" +
"<HEAD><TITLE>" + title + "</TITLE></HEAD>\n" +
"<BODY BGCOLOR=\"#FDF5E6\">\n" +
"<H1 ALIGN=CENTER>" + title + "</H1>\n" +
"<B>Based upon extensive research of " +
"astro-illogical trends, psychic farces, " +
"and detailed statistical claptrap, " +
"we have chosen the " + numbers.length +
" best lottery numbers for you.</B>" +
"<OL>");
for(int i=0; i<numbers.length; i++) {
out.println(" <LI>" + numbers[i]);
}
out.println("</OL>" +
"</BODY></HTML>");
}
/** The standard service method compares this date against
* any date specified in the If-Modified-Since request header.
* If the getLastModified date is later or if there is no
* If-Modified-Since header, the doGet method is called
* normally. But if the getLastModified date is the same or
* earlier, the service method sends back a 304 (Not Modified)
* response and does <B>not</B> call doGet. The browser should
* use its cached version of the page in such a case.
*/
public long getLastModified(HttpServletRequest request) {
return(modTime);
}
// A random int from 0 to 99.
private int randomNum() {
return((int)(Math.random() * 100));
}
}
Initializations Controlled by Initialization Parameters
写道
1. Use the web.xml servlet element to give a name to your servlet.
2. Use the web.xml servlet-mapping element to assign a custom
URL to your servlet. You never use default URLs of the form
http://.../servlet/ServletName when using init parameters. In fact,
these default URLs, although extremely convenient during initial
development, are almost never used in deployment scenarios.
3. Add init-param subelements to the web.xml servlet element to
assign names and values of initialization parameters.
4. From within your servlet’s init method, call getServletConfig
to obtain a reference to the ServletConfig object.
5. Call the getInitParameter method of ServletConfig with the
name of the init parameter. The return value is the value of the init
parameter or null if no such init parameter is found in the web.xml
file.
3.6.4 The destroy Method
分享到:
相关推荐
servlet-api-2.5.jar和jsp-api-2.1.jar
一个免费的教程。PDF文件,英文。 关于SERVLET和JSP一本很好的教材 虽然老了点,但是讲得很不错,很多例子帮助理解。
解决如下异常 java.lang.NoClassDefFoundError: javax/servlet/jsp/jstl/core/ConditionalTagSupport 的解决办法。
书中的第2卷。 高级主题 英文PDF和CHM格式
Servlet与JSP核心编程第二版第一卷英文版,Core Servlets and JavaServer Pages(Volume.1 .2nd.Edition),自己制作的,中英文对照着看。
jsp_servlet_javabean网上订餐系统 摘 要 二十一世纪是一个集数字化,网络化,信息化的,以网络为核心的社会。中国的网民 充分领略到"畅游天地间,网络无极限" 所带来的畅快。随着Internet的飞速发展,使得网络的...
书中的第2卷。 高级主题 英文PDF和CHM格式
《servlet与jsp核心编程 第2版 卷一》英文版,《Core Servlets and Javaserver Pages V1》,卷一,文字版,非扫描版。
本书共分4部分,从xml、servlet、jsp和应用的角度向读者展示了java web开发中各种技术的应用,循序渐进地引导读者快速掌握java web开发。. 本书内容全面,涵盖了从事java web开发所应掌握的所有知识。在知识的讲解...
感谢弟兄共享《servlet与JSP核心编程》,现将源代码奉上,希望对学习WEB开发的人提供便利。
《servlet与jsp核心编程 第2版 卷二》英文版,《Core Servlets and Javaserver Pages V2》文字版,非扫描版
本书共分4部分,从xml、servlet、jsp和应用的角度向读者展示了java web开发中各种技术的应用,循序渐进地引导读者快速掌握java web开发。. 本书内容全面,涵盖了从事java web开发所应掌握的所有知识。在知识的讲解...
fckeditor-java-core-2.4.jar <servlet> <servlet-name>Connector</servlet-name> <servlet-class>net.fckeditor.connector.ConnectorServlet</servlet-class> <load-on-startup>1</load-on-startup> </...
java.lang.NoClassDefFoundError: javax/servlet/jsp/jstl/core/ConditionalTagSupport 问题-附件资源
本书共分4部分,从xml、servlet、jsp和应用的角度向读者展示了java web开发中各种技术的应用,循序渐进地引导读者快速掌握java web开发。. 本书内容全面,涵盖了从事java web开发所应掌握的所有知识。在知识的讲解...
javaweb课程设计基础知识+教程+课件,涵盖: 适合人群:大学生、毕业生、...---Jdbc1.ppt ---Jdbc2.ppt ---Jdbc3.ppt ---jsp.ppt ---_事件监听器.ppt ---_国际化11.ppt --- 文件的上传和下载ppt --- 标签与简单标签ppt
基于 MySQL+Tomcat+Servlet+JSP 的简单作业管理系统源码+数据库+项目说明.zip # HomeworkManagement ## 简介 一个基于 MySQL+Tomcat+Servlet+JSP 的简单作业管理系统 --- ## 更新 ### Ver 5.0 - 所有模块...
本书共分4部分,从xml、servlet、jsp和应用的角度向读者展示了java web开发中各种技术的应用,循序渐进地引导读者快速掌握java web开发。. 本书内容全面,涵盖了从事java web开发所应掌握的所有知识。在知识的讲解...
jakarta-taglibs-standard-current (JSP 标准标签库(JSTL) )