以登录实例简单介绍Servlet使用

1.简单介绍

   Java Servlet 是执行在 Web server或应用server上的程序,使用 Servlet。您能够收集来自网页表单的用户输入。呈现来自数据库或者其它源的记录。还能够动态创建网页。

   作用:Web 浏览器的请求和 HTTP server上的数据库或应用程序之间的中间层

   简单说就是接受client传递的输入然后进行处理(链接数据库等)然后在将对应的结果(html等)在返回给client显示相当于MWC中的Ccontroller层。


   Servlet程序的基本执行过程时序图:以下通过一个登陆实例来简单说明下。



2.登录实例

   编写步骤:
      1.建立LoginSerlvet并继承HttpServlet
      2.覆盖doGet或doPost方法,编译
      3.在web.xml配置文件里进行配置
      4.写html登录页面并放入Tomcat中webapps中相应web项目中
      5.启动《Tomcatserver》,输入网址:http://127.0.0.1:8080/text_servlet/login.html执行程序

《apache-tomcat-5.5.26》中有相应的程序,配置好Tomcat相应的环境变量就能够执行。

     


 LoginServlet.java

<span style="font-family:SimSun;font-size:14px;">
import javax.servlet.http.*;
import javax.servlet.*;

public class LoginServlet extends HttpServlet{
	
	protected void doGet(HttpServletRequest request,
                     HttpServletResponse response)
              throws ServletException,
                     java.io.IOException{
						 
			String username = request.getParameter("username");
			String password = request.getParameter("password");
			
			System.out.println("username"+username);
			System.out.println("password"+password);
			
			response.setContentType("text/html");
			response.getWriter().println("login Success!");						 
					 }
}</span>

 web.xml

<span style="font-family:SimSun;font-size:14px;">
<?xml version="1.0" encoding="ISO-8859-1"?

> <web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" version="2.4"> <servlet> <servlet-name>MyServlet</servlet-name> <!--servlet内部名称--> <servlet-class>LoginServlet</servlet-class> <!--自己编写的servlet类名称。假设有包必须写完毕--> </servlet> <servlet-mapping> <servlet-name>MyServlet</servlet-name> <!--servlet内部名称--> <url-pattern>/loginServlet</url-pattern> <!--供client訪问的名称必须以/开头--> </servlet-mapping> </web-app> </span>

 login.html

<span style="font-family:SimSun;font-size:14px;">
<html>
	<head>
	<title>登录</title>
	
	</head>
	<body>
		<form action="loginServlet">
		用户:<input type="text" name="username"><br>
		密码:<input type="text" name="password"><br>
		<input type="submit" value="登录">
		</form> 
	</body>
</html></span>

执行结果图:



时序图:


    对第7步:service的说明:在第6步中在对LoginService实例化后会调用调用父类HttpServlet中service方法来确定client进行的是dopost提交还是doget提交,然后在转到子类相应的方法。假设子类中没有相应的提交方法,会调用HttpServlet中的父类的方法。

HttpServletRequest: 包装client提交过来的全部数据。1.client的IP地址  2 .client的表单数据  3.Cookies信息 
HttpServletResponse:包装了向client写出的数据。    1.将数据库信息输出  2.向client输出图片html等


3.servlet生命周期
 生命全过程:
    1.载入Servlet并实例化 new
    2.初始化 init
    3.处理请求 service doGet doPost
    4.退出服务 destroy()

TestLifeCycleServlet实例:

import java.io.*;
import javax.servlet.http.*;
import javax.servlet.*;


public class TestLifeCycleServlet extends HttpServlet{
        //1.构造函数
	public TestLifeCycleServlet(){
		 System.out.println("-------------TestLifeCycleServlet()----------------");
	}
	//2.init方法
	public void init(){
		 System.out.println("-------------init()----------------");
	}
	//3.doGet方法
	protected void doGet(HttpServletRequest req,
                     HttpServletResponse resp)
              throws ServletException,
                     java.io.IOException{
		 System.out.println("-------------doGet()----------------");	 
					 }
        //4.销毁时调用的方法
        public void destroy(){
		
	}				 
}

输入:http://127.0.0.1:8080/text_servlet/TestLifeCycleServlet。

记得将生成的.class文件放在Tomcat相应的文件夹和配置相应的web.xml文件。


执行结果为:


特点:单实例多线程

     TestLifeCycleServlet会在第一次使用的时候new,并且在new完该Servlet后。会调用init方法,new和init方法仅仅一次。以后不再实例化,所以Servlet是单实例多线程的,Servlet不是线程安全的。所以一般不定义成员变量。



原文地址:https://www.cnblogs.com/lytwajue/p/7284707.html