Java Servlet-入门

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java Servlet-入门相关的知识,希望对你有一定的参考价值。

技术分享

技术分享

技术分享

配置 servlet

技术分享
 1 <servlet>
 2     <servlet-name>one</servlet-name>
 3     <servlet-class>servletlearn.FirstServlet</servlet-class>
 4 </servlet>
 5 <servlet-mapping>
 6     <servlet-name>one</servlet-name>
 7     <url-pattern>/test</url-pattern>
 8     <url-pattern>/test1</url-pattern>
 9 </servlet-mapping>
10   
11 <servlet>
12     <servlet-name>two</servlet-name>
13     <servlet-class>servletlearn.SecondServlet</servlet-class>
14 </servlet>
15 <servlet-mapping>
16     <servlet-name>two</servlet-name>
17     <url-pattern>/test2</url-pattern>
18 </servlet-mapping>
web.xml

FirstServlet.java

技术分享
package servletlearn;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class FirstServlet extends HttpServlet {
    protected void service(HttpServletRequest arg0, HttpServletResponse arg1) throws ServletException, IOException {
        System.out.println("我的第一个servlet程序运行啦!!");
    }
}
FirstServlet.java

SecondServlet.java

package servletlearn;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class SecondServlet extends HttpServlet {
    protected void service(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
        System.out.println("我的第二个servlet!!!");
        System.out.println("客户端浏览器类型:"+req.getHeader("User-Agent"));
        res.getWriter().println("<h1>Hello Servelet!!!</h1>");
        res.getWriter().println("<h2>Hello Servelet!!!</h2>");
    }
}

 

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

Servlet快速入门

Java 之 Servlet 基础入门

Java Servlet-入门

java web入门-servlet初步

Java基础——JSP

Servlet入门