第一个sevlet应用
环境:jdk1.7.0_45 tomcat-7.0.54
方法一:
1、新建一个Dynamic web project(工程名:TestWev)
2、新建一个servlet继承GenericServlet(包名:com.test,类名:FirstServlet)
特别注意:注释掉@WebServlet("/FirstServlet"),这里的配置和web.xml重复,冲突了。如果是servlet3.0以上的版本就在这里配置就够了,不需要配置web.xml了(如:方法二)
3、在service方法里,写上需要和页面交互的东西。
package com.test; import java.io.IOException; import java.io.OutputStream; import javax.servlet.GenericServlet; import javax.servlet.ServletException; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; import javax.servlet.annotation.WebServlet; /** * Servlet implementation class FirstServlet */ //@WebServlet("/FirstServlet") public class FirstServlet extends GenericServlet { private static final long serialVersionUID = 1L; /** * @see GenericServlet#GenericServlet() */ public FirstServlet() { super(); // TODO Auto-generated constructor stub } /** * @see Servlet#service(ServletRequest request, ServletResponse response) */ public void service(ServletRequest request, ServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub OutputStream outputStream = response.getOutputStream(); outputStream.write("hello,world".getBytes()); } }
4、新建web.xml(有可能在WebContent/WEB-INF目录下有,那就不用新建了),在web.xml中配置:
servlet-name:自定义命名,前后一样就行
url-class:包名+类名,不能有错
url-pattern:自定义命名,随后在浏览器输入的地址后面就是带着个名字就对应到你想要的地址
<?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/j2ee" xmlns:web="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" version="2.4"> <servlet> <servlet-name>FirstServlet</servlet-name> <servlet-class>com.test.FirstServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>FirstServlet</servlet-name> <url-pattern>/FirstServlet</url-pattern> </servlet-mapping> </web-app>
4、启动tomcat,完了在浏览器输入:http://localhost:8080/+工程名+/+url-pattern,回车
方法二
在方法一的基础上,不要注释"@WebServlet("/FirstServlet")",并且删掉web.xml
然后启动tomcat,其他完全一样的。
注意:保证你配置的jdk里面的servlet是3.0以上的版本。一般servlet在jdk的Java\jdk1.7.0_45\lib\missioncontrol\plugins这个目录下。
方法三:
同方法二,只不过继承的是HttpServlet(他本身就是GernericServlet的子类),用来处理http请求最合理
1、新建servlet,命名为ServletDemo2,继承HttpServlet
2、在doGet()中写与浏览器的交互
3、在doPost()调用doGet(request,response);
package com.test; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * Servlet implementation class ServletDemo2 */ @WebServlet("/ServletDemo2") public class ServletDemo2 extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public ServletDemo2() { super(); // TODO Auto-generated constructor stub } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub response.getOutputStream().write("hello".getBytes()); } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub doGet(request, response); } }