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

 

转载于:https://www.cnblogs.com/mxzer/p/6433880.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值