Servlet簡單實現登錄功能
本文實例為大家分享了Servlet簡單實現登錄功能的具體代碼,供大家參考,具體內容如下
介紹:
Servlet 是 JavaWeb 三大組件之一。三大組件分別是:Servlet 程序、Filter 過濾器、Listener 監(jiān)聽器。Servlet 是運行在服務器上的一個 java 小程序,它可以接收客戶端發(fā)送過來的請求,并響應數據給客戶端。
學習內容:
1、編寫Servlet程序
2、web.xml 中去配置 servlet
3、簡單實現登錄功能
具體步驟:
1.創(chuàng)建web工程,目錄如下

2.創(chuàng)建一個登錄頁面,一個登陸成功頁面放在web目錄下

登錄界面代碼如下
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<html >
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<form action="http://localhost:8080/demo_war_exploded/loginServlet" method="post">
<span class="errorMsg">
${empty requestScope.Msg? "請輸入用戶名和密碼":requestScope.Msg}
</span><br>
用戶名:<input type="text" name="username"><br>
密碼:<input type="password" name="password"><br>
<input type="submit" value="登錄">
</form>
</body>
</html>
3.創(chuàng)建LoginServlet類,繼承HttpServlet
LoginServlet.java
因為這里只是簡單介紹一下servlet用法,所以把用戶名和密碼是寫的固定的。
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
public class LoginServlet extends HttpServlet {
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//設置請求發(fā)來的字符集,避免亂碼
req.setCharacterEncoding("UTF-8");
//獲取用戶名和密碼
String username=req.getParameter("username");
String password=req.getParameter("password");
//判斷用戶名密碼是否正確(這里只做簡單講解)
if(username.equals("張三")&&password.equals("123456")){
//如果用戶名密碼正確,則請求轉發(fā)到登錄成功頁面
req.getRequestDispatcher("success.html").forward(req,resp);
}else{
//否則重定向到登錄界面,并提示用戶用戶名或密碼錯誤
req.setAttribute("Msg","用戶名或密碼錯誤");
req.getRequestDispatcher("index.jsp").forward(req,resp);
}
}
}
4.在web.xml文件中配置Servlet
我們配置的時候只需要添加Servlet部分代碼,其他的在創(chuàng)建時已經自動生成。Servlet-name 是要配置的類名,Servlet-class是類的全類名(不懂的朋友,在可以直接寫類名,系統(tǒng)會提示),最重要的:url-pattern標簽配置訪問地址 ,/ 斜杠在服務器解析的時候,表示地址為:http://ip:port/工程路徑, /hello 表示地址為:http://ip:port/工程路徑/hello 。
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" version="4.0"> <servlet> <servlet-name>LoginServlet</servlet-name> <servlet-class>LoginServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>LoginServlet</servlet-name> <url-pattern>/loginServlet</url-pattern> </servlet-mapping> </web-app>
實現結果
運行程序,輸入正確的用戶名密碼登陸成功后,會跳轉到登錄成功頁面


輸入錯誤的用戶名密碼,會跳轉回登錄頁面,并提示用戶“用戶名或密碼錯誤”


小結:
通過今天的學習,我們需要掌握Javaweb工程的創(chuàng)建以及實現了一個簡單的Servlet程序,并學會在web.xml文件中配置Servlet類??梢詫崿F簡單的登錄,學習了請求轉發(fā)的使用,最后了解了如何設置字符集。
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
java ssm框架的controller實現向頁面?zhèn)鬟f參數
這篇文章主要介紹了java ssm框架的controller實現向頁面?zhèn)鬟f參數,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-05-05
Intellij idea下使用不同tomcat編譯maven項目的服務器路徑方法詳解
今天小編就為大家分享一篇關于Intellij idea下使用不同tomcat編譯maven項目的服務器路徑方法詳解,小編覺得內容挺不錯的,現在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧2019-02-02

