Servlet(20分)

  • 主题发起人 主题发起人 冰冰
  • 开始时间 开始时间

冰冰

Unregistered / Unconfirmed
GUEST, unregistred user!
本人初学java,请问,怎样启动一个Serverlet程序?
 

需要配置web.xml文件 以下是简单的范例
testServlet.java 代码如下 :
package dzsb.web.user;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
/**
* <p>Title: dzsb</p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2003</p>
* <p>Company: sun</p>
* @author michael.sun
* @version 1.0
*/
public class testServlet extends HttpServlet {
private static final String CONTENT_TYPE = "text/html;
charset=GBK";
//Initialize global variables
public void init() throws ServletException {
}
//Process the HTTP Get request
public voiddo
Get(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType(CONTENT_TYPE);
PrintWriter out = response.getWriter();
out.println("<html>");
out.println("<head><title>testServlet</title></head>");
out.println("<body bgcolor=/"#ffffff/">");
out.println("<p>The servlet has received a GET. This is the reply.</p>");
out.println("</body></html>");
}
//Process the HTTP Post request
public voiddo
Post(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType(CONTENT_TYPE);
PrintWriter out = response.getWriter();
out.println("<html>");
out.println("<head><title>testServlet</title></head>");
out.println("<body bgcolor=/"#ffffff/">");
out.println("<p>The servlet has received a POST. This is the reply.</p>");
out.println("</body></html>");
}
//Clean up resources
public void destroy() {
}
}
web.xml文件如下
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
<servlet>
<servlet-name>testservlet</servlet-name>
<servlet-class>dzsb.web.user.testServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>testservlet</servlet-name>
<url-pattern>/testservlet</url-pattern>
</servlet-mapping>
</web-app>
 
后退
顶部