第一个servlet小程序
com.fry.servlet.HelloServlet
1 package com.fry.servlet; 2 3 import javax.servlet.ServletException; 4 import javax.servlet.http.HttpServlet; 5 import javax.servlet.http.HttpServletRequest; 6 import javax.servlet.http.HttpServletResponse; 7 import java.io.IOException; 8 import java.io.PrintWriter; 9 10 public class HelloServlet extends HttpServlet { 11 12 @Override 13 protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { 14 System.out.println("接收到get请求!!!"); 15 PrintWriter out=resp.getWriter(); 16 resp.setContentType("text/html;charset=utf-8"); 17 out.println("<strong>服务器已经接收到get请求!!!!!!</strong><br>"); 18 } 19 20 @Override 21 protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { 22 System.out.println("接收到post请求!!!"); 23 } 24 }
index.jsp
1 <%-- 2 Created by IntelliJ IDEA. 3 User: user 4 Date: 2017/10/31 5 Time: 13:46 6 To change this template use File | Settings | File Templates. 7 --%> 8 <%@ page contentType="text/html;charset=UTF-8" language="java" %> 9 <html> 10 <head> 11 <title>$Title$</title> 12 </head> 13 <body> 14 欢迎访问我的第一个JSP。<br> 15 <a href="hi">get请求</a> 16 </body> 17 </html>
WEB-INF/web.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" 5 version="3.1"> 6 <servlet> 7 <servlet-name>helloServlet</servlet-name> 8 <servlet-class>com.fry.servlet.HelloServlet</servlet-class> 9 </servlet> 10 <servlet-mapping> 11 <servlet-name>helloServlet</servlet-name> 12 <url-pattern>/hi</url-pattern> 13 </servlet-mapping> 14 </web-app>
目录结构