首页 > 学院 > 开发设计 > 正文

servlet使用入门

2019-11-06 06:02:55
字体:
来源:转载
供稿:网友

创建web工程servlet,然后新建TestServlet.java

package com.xmyself.servlet; import java.io.IOException; import javax.servlet.ServletConfig;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse; public class TestServlet extends HttpServlet {    PRivate static final long serialVersionUID = 1L;     public void init(ServletConfig config) throws ServletException {    }     public void doPost(HttpServletRequest request, HttpServletResponse response)            throws IOException, ServletException {    }     public void doGet(HttpServletRequest request, HttpServletResponse response)            throws IOException, ServletException {        response.getWriter().write("test servlet");    }     public void destroy() {    }}然后,修改web.xml
<?xml version="1.0" encoding="UTF-8"?><web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"    xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"    version="2.5">    <display-name>servlet</display-name>         <servlet>        <servlet-name>test</servlet-name>        <servlet-class>com.xmyself.servlet.TestServlet</servlet-class>    </servlet>    <servlet-mapping>        <servlet-name>test</servlet-name>        <url-pattern>/test</url-pattern>    </servlet-mapping></web-app>启动servlet工程,浏览器访问,出现“test servlet”
http://localhost:8080/servlet/test
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表