登錄功能實現_Servlet開發規則

servlet 

  • tomcat是啓動servlet的容器
  • servlet是服務端的一個小程序
  • 處理客戶端的請求與相應

 添加源碼

處理登錄請求web.xml配置

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns="http://xmlns.jcp.org/xml/ns/javaee"
	xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
	id="WebApp_ID" version="4.0">
	<!-- 配置servlet: 配置LoginServlet與請處理請求的映射 -->
	<servlet>
		<servlet-name>loginServlet</servlet-name>
		<servlet-class>com.xiaowang.login.servlet.LoginServlet</servlet-class>
	</servlet>
	<servlet-mapping>
		<servlet-name>loginServlet</servlet-name>
	<!-- 客戶端的登錄請求: http://locallost:8080/web01/login-->
		<url-pattern>/login</url-pattern>
	</servlet-mapping>

</web-app>

LoginServlet

package com.xiaowang.login.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class LoginServlet  extends HttpServlet{
	/**
	 * 經常使用方法:doGet  doPost service
	 * doGet:處理客戶端get方式的請求
	 * doPost:處理客戶端Post方式的請求
	 * service:根據客戶端的請求方式去調用對應的doGet、doPost方法
	 */
	
	//要麼重寫service,要麼重寫doGet  doPost方法
	
	@Override
	protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// TODO Auto-generated method stub
		super.service(req, resp);
	}
	
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
	 doPost(req,resp);
	}
	
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
	
		System.out.println("登錄請求過來了............");
		
	}
	
	
	
	
	
	
	
}
相關文章
相關標籤/搜索