JSP基础知识(servlet)

1.servlet
servlet是一个java程序,是在服务器上运行以处理客户端请求并做出响应的程序
servlet生命周期
生命周期各个阶段
实例化--->servlet容器创建servlet的实例
初始化--->该容器调用init()方法
请求处理--->如果请求servlet,则容器调用service()方法
服务终止--->销毁实例之前调用destroy()方法
配置servlet
1.在web.xml中配置servlet
<!-- 配置servlet -->
<servlet>
<!-- 映射名称 -->
<servlet-name>HelloServlet</servlet-name>
<!-- 具体的servlet类 -->
<servlet-class>com.jredu.j2ee.ch04.HelloServlet</servlet-class>
</servlet>
<servlet-mapping>
<!-- 映射名称 -->
<servlet-name>HelloServlet</servlet-name>
<!-- 映射路径(虚拟路径) -->
<url-pattern>/hello</url-pattern>
</servlet-mapping>
2.使用注解的方式配置servlet
使用注解WebServlet:
Name
urlPatterns、Value:配置servlet
loadOnStartup:添加这个参数后可以设置servlet立即运行
initParams:设置初始化参数

设置输入输出为中文:
//将输入转换为中文
request.setCharacterEncoding("UTF-8");
//设置输出为中文
response.setContentType("text/html;charset=UTF-8");
response.setCharacterEncoding("UTF-8");

//name:servlet-name
//value、urlPatterns:url-pattern
@WebServlet(urlPatterns={"/annotation","/a"})
public class AnnotatioonServlet extends HttpServlet {

/**
* Constructor of the object.
*/
public AnnotatioonServlet() {
super();
}

/**
* Destruction of the servlet. <br>
*/
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}

/**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

response.setCharacterEncoding("UTF-8");
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
out.println("<HTML>");
out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>");
out.println(" <BODY>");
out.print(" This is ");
out.print(this.getClass());
out.println(", using the GET method");
out.println(" </BODY>");
out.println("</HTML>");
out.flush();
out.close();
}

/**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to post.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}

/**
* Initialization of the servlet. <br>
*
* @throws ServletException if an error occurs
*/
public void init() throws ServletException {
// Put your code here
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值