Servlet使用

Posted wccw

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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();}
}

 

以上是关于Servlet使用的主要内容,如果未能解决你的问题,请参考以下文章

MySQL SELECT with LIKE in java Servlet MySQL 错误

jsp初识servlet转发转发与重定向的比较

servlet和filter的区别

JSP基础学习

Java基础——JSP

servlet,filter,listener,intercepter区别