码迷,mamicode.com
首页 > 其他好文 > 详细

Servlet使用

时间:2020-05-28 00:58:49      阅读:57      评论:0      收藏:0      [点我收藏+]

标签:文件处理   splay   bsp   ppi   vax   text   nbsp   att   super   

导入 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();}
}

 

Servlet使用

标签:文件处理   splay   bsp   ppi   vax   text   nbsp   att   super   

原文地址:https://www.cnblogs.com/wccw/p/12977441.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!