手工编写servlet

index.jsp:

 1 <%@ page language="java" import="java.util.*" contentType="text/html; charset=utf-8"%>
 2 <%
 3 String path = request.getContextPath();
 4 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
 5 %>
 6 
 7 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
 8 <html>
 9   <head>
10     <base href="<%=basePath%>">
11     
12     <title>My JSP 'index.jsp' starting page</title>
13     <meta http-equiv="pragma" content="no-cache">
14     <meta http-equiv="cache-control" content="no-cache">
15     <meta http-equiv="expires" content="0">    
16     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
17     <meta http-equiv="description" content="This is my page">    
18   </head>
19   
20   <body>
21    <h1>第一个Servlet小例子</h1>
22    <hr>
23    <a href="servlet/HelloServlet">get方式请求HelloServlet</a><br>
24    <form action="servlet/HelloServlet" method="post">
25        <input type="submit" value="用post方式请求HelloServlet" >
26        </form>
27   </body>
28 </html>

web.xml:

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <web-app version="2.5" 
 3     xmlns="http://java.sun.com/xml/ns/javaee" 
 4     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
 5     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
 6     http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
 7   <display-name></display-name>    
 8   <welcome-file-list>
 9     <welcome-file>index.jsp</welcome-file>
10   </welcome-file-list>
11   
12   <servlet>
13       <servlet-name>HelloServlet</servlet-name>
14       <servlet-class>servlet.HelloServlet</servlet-class>
15   </servlet>
16   <servlet-mapping>
17       <servlet-name>HelloServlet</servlet-name>
18       <!-- 和界面上的超链接一一对应 -->
19       <url-pattern>/servlet/HelloServlet</url-pattern>
20   </servlet-mapping>
21 </web-app>

HelloServlet.java

 1 package servlet;
 2 
 3 import java.io.IOException;
 4 import java.io.PrintWriter;
 5 
 6 import javax.servlet.ServletException;
 7 import javax.servlet.http.HttpServlet;
 8 import javax.servlet.http.HttpServletRequest;
 9 import javax.servlet.http.HttpServletResponse;
10 
11 public class HelloServlet extends HttpServlet{
12 
13     @Override
14     protected void doGet(HttpServletRequest request, HttpServletResponse response)
15             throws ServletException, IOException {
16         System.out.println("处理get()请求......");
17         PrintWriter out=response.getWriter();
18         response.setContentType("text/html;charset=utf-8");
19         out.println("<strong>Hello Servet!</strong><br>");
20     }
21 
22     @Override
23     protected void doPost(HttpServletRequest request, HttpServletResponse response)
24             throws ServletException, IOException {
25         System.out.println("处理post()请求......");
26         PrintWriter out=response.getWriter();
27         response.setContentType("text/html;charset=utf-8");
28         out.println("<strong>Hello Servet!</strong><br>");        
29     }
30     
31 }
原文地址:https://www.cnblogs.com/guoyansi19900907/p/4344268.html