JSP基础知识(servlet)
2017-09-11 18:39
495 查看
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
}
}
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
}
}
相关文章推荐
- Servlet&JSP学习笔记:Web应用程序基础知识
- jsp&servlet基础知识
- Servlet和JSP知识复习(2)Servlet基础
- 关于jsp servlet和jfinal的基础知识
- JSP基础知识—执行过程,脚本元素,与Servlet的区别
- 【Servlet和JSP-学习-1】基础知识
- Head First Servlet&Jsp 学习笔记(一) 一些基础知识
- JSP Servlet基础知识(二)
- JSP与Servlet 基础知识
- Java之JSP和Servlet基础知识。
- JSP SERVLET 基础知识
- Java基础知识强化100:jsp和servlet有什么区别
- JSP+Servlet+JavaBean基础知识
- JSP基础知识(Servlet相关接口)
- servlet 和 jsp基础知识复习
- ServLet中的基础知识
- servlet基础知识
- servlet基础知识
- Jsp学习之Servlet基础
- JSP的一些基础知识