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、编码前段页面
登陆页面:
成功登陆之后的页面:
4、在WebRoot(eclipse中式WebCotent)/WEB-INF/web.xml中添加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"%>
<!DOCTYPE HTML>
<html>
<head>
<script>
function check(){//判空检测
var username = document.getElementById("username");
var password = document.getElementById("password");
var textnode1 = document.createTextNode("请输入有效的用户名");
var textnode2 = document.createTextNode("请输入正确的密码");
if(document.getElementById("p1").hasChildNodes()){
document.getElementById("p1").removeChild(document.getElementById("p1").firstChild);
}
if (username.value == ""){
document.getElementById("p1").appendChild(textnode1);
}
if(document.getElementById("p2").hasChildNodes()){
document.getElementById("p2").removeChild(document.getElementById("p2").firstChild);
}
if(password.value == ""){
document.getElementById("p2").appendChild(textnode2);
}
if(password.value != "" && username.value != "")
{
var login = document.getElementById('loginform');
login.submit();
}
}
</script>
</head>
<body>
This is my first JSP page. <p/>
<form action='servletdemo' method='post' id='loginform'>
用户名:<input type="text" id="username" name="username"><a id="p1" ></a><p/>
密 码:<input type="password" id="password" name="password"><a id ="p2" ></a><p/>
<input type="button" value="登陆" οnclick="check()">
<input type="button" value="注册" οnclick="regin()">
</form>
</body>
</html>
成功登陆之后的页面:
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<!DOCTYPE HTML>
<html>
<head>
</head>
<body>
This is my second JSP page. <p/>
<%
String name = (String)session.getAttribute("username");
String pass = (String)session.getAttribute("password");
%>
welcome <%=name%>!<p/>
you password is <%=pass%>
</body>
</html>
4、在WebRoot(eclipse中式WebCotent)/WEB-INF/web.xml中添加servlet部署
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>servletTest</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<servlet>
<description>This is the description of my J2EE component</description>
<display-name>This is the display name of my J2EE component</display-name>
<servlet-name>servletdemo</servlet-name>
<servlet-class>com.servlet.servletdemo</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>servletdemo</servlet-name>
<url-pattern>/servletdemo</url-pattern>//此处“/”后的名字和jsp页面中form表单的action保持一致
</servlet-mapping>
</web-app>
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()方法!用来进行销毁实例的工作");
}
}