首页 > 代码库 > Java Servlet-入门
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>
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程序运行啦!!"); }}
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-入门
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。