Servlet

servlet的编程步骤:

1:建立servlet类

package com.baidu.one;

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 TwoServletDemo extends HttpServlet {

 public TwoServletDemo() {
  super();
 }

 public void doGet(HttpServletRequest request, HttpServletResponse response)
   throws ServletException, IOException {
  response.getWriter().write("TwoServletDemo");
 }

 public void doPost(HttpServletRequest request, HttpServletResponse response)
   throws ServletException, IOException {
  doGet(request, response);
 }

}

2:写web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 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_2_5.xsd">
 <!--
 <servlet>
  <servlet-name>OneServlet</servlet-name>
  <servlet-class>com.baidu.one.ServletDemo</servlet-class>
 </servlet>
 <servlet-mapping>
  <servlet-name>OneServlet</servlet-name>
  <url-pattern>/hell</url-pattern>
 </servlet-mapping>
   TwoServletDemo  -->
 <servlet>
  <servlet-name>TwoServletDemo</servlet-name>
  <servlet-class>com.baidu.one.TwoServletDemo</servlet-class>
 </servlet>
 <servlet-mapping>
  <servlet-name>TwoServletDemo</servlet-name>
  <url-pattern>/servlet/TwoServletDemo</url-pattern>
 </servlet-mapping>
</web-app>

3:布署到tomcat上

4:访问

 

posted @ 2015-10-11 01:11  Sunray0330  阅读(111)  评论(0编辑  收藏  举报