Servlet实例
开发环境:
eclipse 5.6
tomcat 8.0
整个项目结构如下:
1.创建项目
打开eclipse,创建J2EE动态web项目:
选择"Dynamic Web Project",点击"Next>"
"Project name"输入项目名称,"Target runtime"选择项目运行在Apache Tomcat v8.0(由于此前eclipse已经配置了tomcat 8.0,若未配置可点击“New Runtime...”按钮进行配置),点击“Next”:
把"build\classes"修改为"WebContent\WEB-INF\classes",点击"Next":
勾选"Generate web.xml deployment descriptor"(注:会在WebContent/WEB-INF目录下生成web.xml文件),点击"Finish”。
2.在src目录下创建com.info.servlet包。
3.在com.info.servlet包下创建FirstServlet.java类.
FirstServlet.java类内容:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 package com.info.servlet; 2 3 import java.io.PrintStream; 4 import javax.servlet.ServletException; 5 import javax.servlet.annotation.WebServlet; 6 import javax.servlet.http.HttpServlet; 7 import javax.servlet.http.HttpServletRequest; 8 import javax.servlet.http.HttpServletResponse; 9 10 @WebServlet(name="firstservlet",urlPatterns={"/firstservlet"}) 11 public class FirstServlet extends HttpServlet { 12 13 public void service(HttpServletRequest request,HttpServletResponse response) 14 throws ServletException,java.io.IOException 15 { 16 try { 17 request.setCharacterEncoding("utf-8"); 18 response.setContentType("text/html;charset=GBK"); 19 20 String userName=request.getParameter("userName"); 21 String gender=request.getParameter("gender"); 22 String age=request.getParameter("age"); 23 24 PrintStream out=new PrintStream(response.getOutputStream()); 25 26 out.println("<html>"); 27 out.println("<head>"); 28 out.println("<title>第一个Servlet程序</title>"); 29 out.println("</head>"); 30 out.println("<body>"); 31 32 33 out.println("用户名: " + userName + "<hr/>"); 34 35 out.println("性别: " + gender + "<hr/>"); 36 37 out.println("年龄: " + age + "<hr/>"); 38 39 40 out.println("</body>"); 41 out.println("</html>"); 42 43 } 44 catch(Exception ex){ 45 //TOO DO 46 throw ex; 47 } 48 } 49 50 }
4.在WebContent目录下创建index.jsp文件
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 <%@ page language="java" contentType="text/html; charset=utf-8" 2 pageEncoding="utf-8"%> 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 4 <html> 5 <head> 6 <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> 7 <title>调用Servlet</title> 8 </head> 9 <body> 10 <form action="firstservlet" method="post"> 11 用户名:<input type="text" name="userName" value=""></input><br/><hr/> 12 性别:<input type="radio" name="gender" value="男">男</input> 13 <input type="radio" name="gender" value="女">女</input> 14 <br/><hr/> 15 年龄:<input type="text" name="age" value=""></input><br/><hr/> 16 17 <input type="submit" value="提交"/> 18 19 </form> 20 </body> 21 </html>
5.运行项目
servletdemo项目上右键点击,选择"Run as" --> "Run on server":
点击"Finish",eclipse会自动打开浏览器插件显示项目首页(也在自行打开浏览器,在地址栏输入http://localhost:8080/servletdemo/)
点击“提交”后,结果如下:
注:
创建项目时,项目选择使用了servlet 3.1规范,从servlet 3.0开始,servlet类可以使用注释来完成配置(见FirstServlet.java文件)。
当然也可以通过web.xml来配置servlet,当web.xml配置了servlet,则servlet中的注释无效。
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1"> <display-name>myssh1</display-name> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> <welcome-file>default.html</welcome-file> <welcome-file>default.htm</welcome-file> <welcome-file>default.jsp</welcome-file> </welcome-file-list> <servlet> <servlet-name>firstservlet</servlet-name> <servlet-class>com.info.servlet.FirstServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>firstservlet</servlet-name> <url-pattern>/firstservlet</url-pattern> </servlet-mapping> </web-app>
实例下载地址: http://pan.baidu.com/s/1qXBlkNq