Servle简介
1.Servlet:使用java代码编写的服务器端小程序;采用多线程处理方式,具有java可移植性特点。
自定义的servlet要继承HttpServlet接口,并覆写其中的doGet()方法;若编译提示找不到Servlet下面包,说明JDK的classpath配置不正确:
javax.servlet或javax.servlet.http不存在。
1 import java.io.IOException; 2 3 import javax.servlet.ServletException; 4 import javax.servlet.http.HttpServlet; 5 import javax.servlet.http.HttpServletRequest; 6 import javax.servlet.http.HttpServletResponse; 7 8 public class HelloServlet extends HttpServlet { 9 10 @Override 11 protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { 12 super.doGet(req, resp); 13 14 HttpSession ss = req.getSession();//获取session对象 15 16 ServletContext sc = super.getServletContext();//获取application 17 } 18 }
在WEB-INFO下的webxml文件配置Servlet映射文件
<servlet> <servlet-name>hello</servlet-name> <servlet-class>marco.basic.servlet.HelloServlet</servlet-class> <!--<load-on-startup>0</load-on-startup>--><!--自动加载--> </servlet> <servlet-mapping><!--<servlet-mapping>可以配置多个--> <servlet-name>hello</servlet-name> <url-pattern>/helloServlet</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>hello</servlet-name> <url-pattern>/hell0/*</url-pattern> </servlet-mapping>
2.Servlet与表单
在浏览器中直接输入地址:属于get请求,会调用doGet()方法;
表单使用post提交时::属于post请求,会调用dopost()方法;
<form action="helloServlet" method="post"> 输入内容:<input type="text" name="info" /> <input type="submit" value="提交" /> </form>
@Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { // super.doGet(req, resp); String info = req.getParameter("info"); //其他操作 ... } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { this.doGet(req, resp);//调用doGet()方法 }
Servlet路径:路径配置错误,会找不到对应的servlet,则报错404;可以在action中设置提交路径:
<form action="<%=request.getContextPath() %>/helloServlet" method="post"> 输入内容:<input type="text" name="info" /> <input type="submit" value="提交" /> </form>
3.Servlet生命周期
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 HelloServlet extends HttpServlet { @Override public void init() throws ServletException { System.out.println("Servlet初始化操作..."); } /* //该service()若存在,则下面的doGet、doPost就无作用;因为在HTTPServlet中已覆写service()方法,已区分区请求的类型:get-->doGet()、post--->doPost() @Override protected void service(HttpServletRequest arg0, HttpServletResponse arg1) throws ServletException, IOException { // TODO Auto-generated method stub super.service(arg0, arg1); } */ @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { // super.doGet(req, resp); String info = req.getParameter("info"); System.out.println(info); //其他操作 //... } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { this.doGet(req, resp);//调用doGet()方法 } @Override public void destroy() { System.out.println("调用销毁方法..."); } }
4.Servlet跳转
客户端跳转:在Servlet中进行客户端跳转时,使用HttpServletResponse中的sendRedirect()方法,只能传递session范围的属性,request属性传递不了。
@Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { HttpSession ss = req.getSession(); ss.setAttribute("name", "mxzer");//session范围属性 req.setAttribute("name2", "mxzer2");//request范围属性 resp.sendRedirect("hello.jsp");//跳转 }
// hello.jsp: name:<%=session.getAttribute("name")%> ---->name:mxzer name2:<%=request.getAttribute("name2")%>---->name2:null
服务器端跳转:使用RequestDispatcher接口中的forward()方法;
@Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { req.getSession().setAttribute("name", "mxzer"); req.setAttribute("name2", "mxzer2"); //实例化RequestDispatcher对象,并传入跳转路径 RequestDispatcher rd = req.getRequestDispatcher("hello.jsp"); rd.forward(req, resp);//服务器端跳转 }
//hello.jsp name:<%=session.getAttribute("name")%> ---->name:mxzer name2:<%=request.getAttribute("name2")%>---->name2:mxzer2
Author