一個(gè)處理用戶登陸的servlet簡(jiǎn)單實(shí)例
本文實(shí)例講述了一個(gè)處理用戶登陸的servlet實(shí)現(xiàn)方法。分享給大家供大家參考。具體分析如下:
Login.java代碼如下:
package com.bai;
import javax.servlet.http.*;
import java.io.*;
public class Login extends HttpServlet{
public void doGet(HttpServletRequest req,HttpServletResponse res){
try{req.setCharacterEncoding("gb2312");
res.setContentType("text/html;charset=gb2312");
PrintWriter pw=res.getWriter();
pw.println("<html>");
pw.println("<body>");
pw.println("<h1>登陸界面</h1>");
pw.println("<form action=logincl method=post>");
pw.println("用戶名:<input type=text name=username><br>");
pw.println("密碼:<input type=password name=passwd><br>");
pw.println("<input type=submit value=login><br>");
pw.println("</form>");
pw.println("</body>");
pw.println("</html>");
}
catch(Exception e){
e.printStackTrace();
}
}
public void doPost(HttpServletRequest req,HttpServletResponse res){
this.doGet(req,res);
}
}
LoginCl.java代碼如下:
package com.bai;
import javax.servlet.http.*;
import java.io.*;
import java.sql.*;
public class LoginCl extends HttpServlet{
public void doGet(HttpServletRequest req,HttpServletResponse res){
Connection conn=null;
Statement stmt=null;
ResultSet rs=null;
String sql = "select username,passwd from users where username = ? and passwd = ?";
try{//req.setCharacterEncoding("gb2312");
String user=req.getParameter("username");
String password=req.getParameter("passwd");
Class.forName("com.mysql.jdbc.Driver");
conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/sqdb","root","root");
// stmt=conn.createStatement();
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setString(1, user);
pstmt.setString(2, password);
rs = pstmt.executeQuery();
// rs=stmt.executeQuery("select top 1 * from users where username='"+user
// +"' and passwd='"+password+"'");
if(rs.next())
{
HttpSession hs=req.getSession(true);
hs.setMaxInactiveInterval(60);
hs.setAttribute("name",user);
res.sendRedirect("welcome?&uname="+user+"&upass="+password);
}
else{
res.sendRedirect("login"); //url
}
}
catch(Exception e){
e.printStackTrace();
}finally{
try{
if(rs!=null){
rs.close();
}
if(stmt!=null){
stmt.close();
}
if(conn!=null){
conn.close();
}
}catch(Exception e){
e.printStackTrace();
}
}
}
public void doPost(HttpServletRequest req,HttpServletResponse res){
this.doGet(req,res);
}
}
其實(shí)上面這個(gè)處理用戶名密碼帶有明顯注入漏洞,可以根據(jù)用戶名從數(shù)據(jù)庫取密碼,用取出的密碼和用戶輸入的密碼比較
sql=select passwd from users where username = ? limit 1
if(rs.next())
{
String passwd=rs.getString(1);
if(passwd.equals(password))
//密碼正確
else //密碼錯(cuò)誤
}
Welcome.java代碼如下:
package com.bai;
import javax.servlet.http.*;
import java.io.*;
public class Welcome extends HttpServlet{
public void doGet(HttpServletRequest req,HttpServletResponse res){
HttpSession hs=req.getSession();
String val=(String)hs.getAttribute("pass");
if(val==null){
try{
System.out.print(1);
res.sendRedirect("login");
}catch(Exception e){
e.printStackTrace();
}
}
String u=req.getParameter("uname");
String p=req.getParameter("upass");
try{//req.setCharacterEncoding("gb2312");
PrintWriter pw=res.getWriter();
pw.println("welcome! "+u+"&pass="+p);
}
catch(Exception e){
e.printStackTrace();
}
}
public void doPost(HttpServletRequest req,HttpServletResponse res){
this.doGet(req,res);
}
}
希望本文所述對(duì)大家的Java程序設(shè)計(jì)有所幫助。
相關(guān)文章
springboot為異步任務(wù)規(guī)劃自定義線程池的實(shí)現(xiàn)
本文主要介紹了springboot為異步任務(wù)規(guī)劃自定義線程池,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-06-06Java實(shí)現(xiàn)滑動(dòng)驗(yàn)證碼的示例代碼
這篇文章主要介紹了Java實(shí)現(xiàn)滑動(dòng)驗(yàn)證碼的示例代碼,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-01-01基于Security實(shí)現(xiàn)OIDC單點(diǎn)登錄的詳細(xì)流程
本文主要是給大家介紹 OIDC 的核心概念以及如何通過對(duì) Spring Security 的授權(quán)碼模式進(jìn)行擴(kuò)展來實(shí)現(xiàn) OIDC 的單點(diǎn)登錄。對(duì)Security實(shí)現(xiàn)OIDC單點(diǎn)登錄的詳細(xì)過程感興趣的朋友,一起看看吧2021-09-09Mybatis中輸入輸出映射與動(dòng)態(tài)Sql圖文詳解
這篇文章主要給大家介紹了關(guān)于Mybatis中輸入輸出映射與動(dòng)態(tài)Sql的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2019-02-02解決執(zhí)行maven命令時(shí)提示Process terminated的問題
這篇文章主要介紹了解決執(zhí)行maven命令時(shí)提示Process terminated的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-09-09springboot如何為web層添加統(tǒng)一請(qǐng)求前綴
這篇文章主要介紹了springboot如何為web層添加統(tǒng)一請(qǐng)求前綴,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-02-02Java實(shí)現(xiàn)調(diào)用外部程序的示例代碼
本文主要介紹了Java實(shí)現(xiàn)調(diào)用外部程序的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-05-05