Servlet代码运用
导入 servlet-api.jar(一般不用导,如果报错就导入,在Tomcat根目录的lib文件夹下)
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为自定义java文件处理 --> <servlet> <servlet-name>Servlet</servlet-name> <display-name>This is the display name of my J2EE component</display-name> <description>This is the description of my J2EE component</description> <servlet-class>com.servlet.Servlet</servlet-class> </servlet> <!-- 只拦截请求,如/login --> <servlet-mapping> <servlet-name>Servlet</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>
Servlet 类
package com.servlet; import java.io.IOException; import javax.servlet.RequestDispatcher; import javax.servlet.ServletContext; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import com.test.jdbc.DBUtil; public class Servlet extends HttpServlet { @Override protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("utf-8"); } //客户端get请求 //public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {} //客户端post请求 //public void doPost(HttpServletRequest req,HttpServletResponse res) throws ServletException,IOException {} //初始 //public void init() throws ServletException{} //销毁 //public void destroy() {super.destroy();} }
结束!