`
onehao
  • 浏览: 13411 次
  • 性别: Icon_minigender_1
  • 来自: 北京
最近访客 更多访客>>
社区版块
存档分类
最新评论

core servlet and JSP ---- 1

阅读更多

 

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

    Servlet-API 2.5与JSP-API 2.1是Java Web开发中两个至关重要的库,主要用于构建基于Java的Web应用程序。这两个jar文件是Java Servlet和JavaServer Pages(JSP)技术的标准实现,由Sun Microsystems(现在是Oracle ...

    jakarta.servlet.jsp.jstl-api-2.0.0.jar

    1. **Core Library**(c:):提供了基本的控制流和数据处理功能,如条件语句(if, choose, when, otherwise)、迭代(forEach, forTokens)、URL重写(redirect, url)等。 2. **Format/Internationalization ...

    CORE SERVLET AND JSP(含代码,基础卷1)

    《CORE SERVLET AND JSP(含代码,基础卷1)》是一本专注于Servlet和JSP技术的经典教程,尽管出版时间较早,但其深入浅出的讲解方式和丰富的实例仍然具有很高的学习价值。Servlet和JSP是Java Web开发中的核心组件,...

    四个jar包commons-io+logging+pool+javax-servlet-jsp-jstl

    标题中的"四个jar包commons-io+logging+pool+javax-servlet-jsp-jstl"提到了四个关键的Java库,这些库在开发Java Web应用程序时非常常见。它们分别是Apache Commons IO、Apache Commons Logging、Apache Commons ...

    Java web常用包javax.servlet.jsp.jstl-1.2.1.zip

    1. **核心标签库(Core Tags)**:这是JSTL中最基本的部分,提供了一系列用于控制流程、输出数据、URL重写等操作的标签。例如,`&lt;c:if&gt;`和`&lt;c:choose&gt;`用于条件判断,`&lt;c:forEach&gt;`用于循环遍历,`&lt;c:set&gt;`和`...

    javax.servlet.jsp.jstl-1.2.zip

    &lt;%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %&gt; List&lt;String&gt; list = Arrays.asList("Item1", "Item2", "Item3"); request.setAttribute("list", list); %&gt; ${list}"&gt; ${item} ``` 在...

    javax/servlet/jsp/jstl/core/ConditionalTagSupport

    在Java Web开发中,`javax.servlet.jsp.jspl.core.ConditionalTagSupport`是JSTL(JavaServer Pages Standard Tag Library)库中的一个核心类,用于支持条件标签的实现。当你遇到`java.lang.NoClassDefFoundError: ...

    Core-Servlets-and-JSP

    《Core-Servlets-and-JSP》是一本深入探讨Servlet和JSP核心技术的书籍,主要针对Web开发中的核心组件进行详细讲解。Servlet和JSP是Java Web开发中的两大基石,它们为构建动态、交互式的Web应用提供了强大的支持。...

    Servlet与JSP核心编程第二版第一卷英文 Core Servlets and JavaServer Pages(Volume.1 .2nd.Edition)

    《Servlet与JSP核心编程第二版第一卷》(Core Servlets and JavaServer Pages Volume 1, 2nd Edition)是一本深入讲解Servlet与JSP技术的专业书籍。本书通过作者Marty Hall亲自开发并教授的课程,为读者提供了丰富的...

    Servlet与JSP核心编程第二版5

    ### Servlet与JSP核心编程第二版5:关键技术与实践解析 #### 一、Servlet概述与核心技术 ##### 1.1 Servlet简介 Servlet 是 Java 平台的一种服务器端技术,用于扩展应用程序的功能。它是一个轻量级的应用程序组件...

    Servlet与JSP核心编程(第2卷 第2版)

    Servlet和JSP(JavaServer Pages)是Java Web开发中的核心技术,用于构建动态、交互式的Web应用程序。《Servlet与JSP核心编程(第2卷 第2版)》这本书深入讲解了这两项技术,帮助开发者理解并掌握其核心概念、用法以及...

    jsp-api文档

    1. **JSP生命周期** JSP页面在服务器上经历了转换、编译、加载、实例化、初始化、服务和销毁等几个阶段。转换阶段,JSP页面被转换为一个Servlet源代码;编译阶段,源代码被编译为字节码;加载和实例化阶段,Servlet...

    jsp-servlet相关资料

    1. **教程书籍:** 《Head First Servlets & JSP》、《Core Servlets and JavaServer Pages》等经典教材。 2. **在线课程:** 课程文件可能包含视频讲解、PPT教程、示例代码等,帮助深入理解JSP和Servlet的使用。 3....

    包括jstl,jsp-api,jdbc4等各种jar文化

    【标题】"包括jstl,jsp-api,jdbc4等各种jar文化" 描述了这个压缩包文件中包含的Java Web开发中的关键库。这些库在Java服务器端编程中扮演着重要角色,尤其对于构建动态网站和Web应用程序。下面将详细阐述每个库的...

    jsp中jstl的jar包

    &lt;jsp-config&gt; &lt;taglib-uri&gt;/WEB-INF/c.tld&lt;/taglib-uri&gt; &lt;taglib-location&gt;/WEB-INF/c.tld&lt;/taglib-location&gt; &lt;taglib-uri&gt;/WEB-INF/fmt.tld&lt;/taglib-uri&gt; &lt;taglib-location&gt;/WEB-INF/fmt.tld&lt;/taglib-...

    Core servlet jsp 中文

    《Core Servlet JSP 中文》是一个深入讲解Servlet和JSP技术的教程,针对初学者和进阶开发者提供全面的知识指导。Servlet是Java Web开发中的基础组件,用于处理HTTP请求并生成动态内容;而JSP(JavaServer Pages)则...

    Core servlets and jsp 源代码

    《Core Servlets and JSP》是一本经典的WEB开发教材,主要涵盖了Servlet和JSP的核心技术。源代码的分享对于深入理解和实践这些技术至关重要。以下是基于提供的文件名所涉及的一些关键知识点: 1. **Ant-Code**:Ant...

    Core Servlet and Jave Server Pages

    《核心Servlet与Java服务器页面》是一本专注于Web开发的经典教程,涵盖了Servlet和JSP(Java Server Pages)的核心概念和技术。这两个技术是Java EE平台的重要组成部分,用于构建动态、交互式的Web应用程序。 ...

Global site tag (gtag.js) - Google Analytics