发现用servlet的比较少哦,看见老一点的程序员都直接在jsp中直接写代码,比我年轻一点的程序员又直接用的框架struts等。而我呢不喜欢用框架,喜欢自己写代码,而且又有强迫症,一定要把服务器代码和html分开,所以我平时就用ajax和servlet最多,其实会熟练使用servlet还是很有必要的,提升开发效率,代码整洁,了解原理都有很大的帮助,下面就一起来学习回顾servlet的使用。
servlet详细使用步骤和代码案例说明:
首先唠叨一下什么是servlet,从最基础最理论的讲一下吧。
servlet是一个java class;
servlet是继承自HttpServlet的class;
不在客户端运行,是在服务端运行,拦截并分发客户端的请求;
相关介绍:
--javax.servlet.*:存放于http协议相关的一般性servlet类
--javax.servlet.http.*:除了继承javax.servlet.* 之外,并且还增加与HTTP协议有关的功能。
(注意:大家有必要学习一下HTTP协议,因为WEB开发都会涉及到)
所有的Servlet 都必须实现javax.servlet.Servlet 接口(Interface)。
若Servlet程序和HTTP 协议无关,那么必须继承javax.servlet.GenericServlet类;
若Servlet程序和HTTP 协议有关,那么必须继承javax.servlet.http.HttpServlet 类。
--HttpServlet:提供了一个抽象类用来创建Http Servlet。
public void doGet()方法:用来处理客户端发出的GET请求
public void doPost()方法:用来处理POST请求
--javax.servlet包的接口:
ServletConfig接口:在初始化的过程中由Servlet容器使用
ServletContext接口:定义Servlet用于获取来自其容器的信息的方法
ServletRequest接口:向服务器请求信息
ServletResponse接口:响应客户端请求
Filter接口:
--javax.servlet包的类:
ServletInputStream类:用于从客户端读取二进制数据
ServletOutputStream类:用于将二进制数据发送到客户端
--javax.servlet.http包的接口:
HttpServletRequest接口:提供Http请求信息
HttpServletResponse接口:提供Http响应
Servlet生命周期
--Servlet生命周期就是指创建Servlet实例后,存在的时间以及何时销毁的整个过程.
--Servlet生命周期有三个方法
init()方法:
service()方法:Dispatches client requests to the protected servicemethod
destroy()方法:Called by the servlet container to indicate to a servlet that the servlet is being taken out of service.
--Servlet生命周期的各个阶段
----实例化:Servlet容器创建Servlet实例
----初始化:调用init()方法
----服务:如果有请求,调用service()方法
----销毁:销毁实例前调用destroy()方法
----垃圾收集:销毁实例
(正式开始.......)
1、在eclipse或者Myeclipse里新建Web Project
2、选择Generate web.xml deployment descriptor
3、编码前段页面
登陆页面:
成功登陆之后的页面:
-
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
-
-
-
-
-
-
-
-
This is my second JSP page.
-
<%
-
String name = (String)session.getAttribute("username");
-
String pass = (String)session.getAttribute("password");
-
%>
-
welcome <%=name%>!
-
you password is <%=pass%>
-
-
复制代码
4、在WebRoot(eclipse中式WebCotent)/WEB-INF/web.xml中添加servlet部署
-
-
-
-
servletTest
-
-
-
index.jsp
-
-
-
-
This is the description of my J2EE component
-
This is the display name of my J2EE component
-
servletdemo
-
com.servlet.servletdemo
-
-
-
-
servletdemo
-
/servletdemo//此处“/”后的名字和jsp页面中form表单的action保持一致
-
-
-
复制代码
5、编码servlet处理class
-
package com.servlet;
-
-
import java.io.IOException;
-
import java.nio.charset.Charset;
-
-
import javax.servlet.ServletException;
-
import javax.servlet.http.HttpServlet;
-
import javax.servlet.http.HttpServletRequest;
-
import javax.servlet.http.HttpServletResponse;
-
import javax.servlet.http.HttpSession;
-
-
public class servletdemo extends HttpServlet{
-
-
/**
-
*
-
*/
-
private static final long serialVersionUID = 1L;
-
-
//初始化
-
public void init() throws ServletException {
-
System.out.println("我是init()方法!用来进行初始化工作");
-
}
-
-
//处理GET请求
-
public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
-
doPost(request, response);
-
}
-
-
//处理POST请求
-
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
-
String username = new String(request.getParameter("username").getBytes("ISO-8859-1"),"utf-8");
-
String password = request.getParameter("password");
-
HttpSession session = request.getSession();
-
session.setAttribute("username", username);
-
session.setAttribute("password", password);
-
response.sendRedirect("/servletTest/success.jsp");
-
System.out.println("用户名:"+username);
-
System.out.println("密码:"+password);
-
}
-
-
//销毁实例
-
public void destroy() {
-
super.destroy();
-
System.out.println("我是destroy()方法!用来进行销毁实例的工作");
-
}
复制代码
成功登陆之后的页面:
-
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
-
-
-
-
-
-
-
-
This is my second JSP page.
-
<%
-
String name = (String)session.getAttribute("username");
-
String pass = (String)session.getAttribute("password");
-
%>
-
welcome <%=name%>!
-
you password is <%=pass%>
-
-
复制代码
4、在WebRoot(eclipse中式WebCotent)/WEB-INF/web.xml中添加servlet部署
-
-
-
-
servletTest
-
-
-
index.jsp
-
-
-
-
This is the description of my J2EE component
-
This is the display name of my J2EE component
-
servletdemo
-
com.servlet.servletdemo
-
-
-
-
servletdemo
-
/servletdemo//此处“/”后的名字和jsp页面中form表单的action保持一致
-
-
-
复制代码
5、编码servlet处理class
-
package com.servlet;
-
-
import java.io.IOException;
-
import java.nio.charset.Charset;
-
-
import javax.servlet.ServletException;
-
import javax.servlet.http.HttpServlet;
-
import javax.servlet.http.HttpServletRequest;
-
import javax.servlet.http.HttpServletResponse;
-
import javax.servlet.http.HttpSession;
-
-
public class servletdemo extends HttpServlet{
-
-
/**
-
*
-
*/
-
private static final long serialVersionUID = 1L;
-
-
//初始化
-
public void init() throws ServletException {
-
System.out.println("我是init()方法!用来进行初始化工作");
-
}
-
-
//处理GET请求
-
public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
-
doPost(request, response);
-
}
-
-
//处理POST请求
-
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
-
String username = new String(request.getParameter("username").getBytes("ISO-8859-1"),"utf-8");
-
String password = request.getParameter("password");
-
HttpSession session = request.getSession();
-
session.setAttribute("username", username);
-
session.setAttribute("password", password);
-
response.sendRedirect("/servletTest/success.jsp");
-
System.out.println("用户名:"+username);
-
System.out.println("密码:"+password);
-
}
-
-
//销毁实例
-
public void destroy() {
-
super.destroy();
-
System.out.println("我是destroy()方法!用来进行销毁实例的工作");
-
}
复制代码
(文章来源:http://www.nei-mao.com/)
成功登陆之后的页面:
-
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
-
-
-
-
-
-
-
-
This is my second JSP page.
-
<%
-
String name = (String)session.getAttribute("username");
-
String pass = (String)session.getAttribute("password");
-
%>
-
welcome <%=name%>!
-
you password is <%=pass%>
-
-
复制代码
4、在WebRoot(eclipse中式WebCotent)/WEB-INF/web.xml中添加servlet部署
-
-
-
-
servletTest
-
-
-
index.jsp
-
-
-
-
This is the description of my J2EE component
-
This is the display name of my J2EE component
-
servletdemo
-
com.servlet.servletdemo
-
-
-
-
servletdemo
-
/servletdemo//此处“/”后的名字和jsp页面中form表单的action保持一致
-
-
-
复制代码
5、编码servlet处理class
-
package com.servlet;
-
-
import java.io.IOException;
-
import java.nio.charset.Charset;
-
-
import javax.servlet.ServletException;
-
import javax.servlet.http.HttpServlet;
-
import javax.servlet.http.HttpServletRequest;
-
import javax.servlet.http.HttpServletResponse;
-
import javax.servlet.http.HttpSession;
-
-
public class servletdemo extends HttpServlet{
-
-
/**
-
*
-
*/
-
private static final long serialVersionUID = 1L;
-
-
//初始化
-
public void init() throws ServletException {
-
System.out.println("我是init()方法!用来进行初始化工作");
-
}
-
-
//处理GET请求
-
public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
-
doPost(request, response);
-
}
-
-
//处理POST请求
-
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
-
String username = new String(request.getParameter("username").getBytes("ISO-8859-1"),"utf-8");
-
String password = request.getParameter("password");
-
HttpSession session = request.getSession();
-
session.setAttribute("username", username);
-
session.setAttribute("password", password);
-
response.sendRedirect("/servletTest/success.jsp");
-
System.out.println("用户名:"+username);
-
System.out.println("密码:"+password);
-
}
-
-
//销毁实例
-
public void destroy() {
-
super.destroy();
-
System.out.println("我是destroy()方法!用来进行销毁实例的工作");
-
}
复制代码
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/29870867/viewspace-1327385/
,如需转载,请注明出处,否则将追究法律责任。