`
onehao
  • 浏览: 13013 次
  • 性别: 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

 

 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics