Servlet+html+css+jsp+mysql实现用户登录
生活随笔
收集整理的這篇文章主要介紹了
Servlet+html+css+jsp+mysql实现用户登录
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
以下是項目結構:
jdbc_demo包是管理mysql數據庫的加載連接和查詢;
Serclet下的Myservle項目是后臺管理用戶請求數據;
以下是Myservlet項目代碼:
package servlet;import jdbc_demo.jdbc; import javax.servlet.*; import javax.servlet.http.*; import javax.servlet.annotation.*; import java.io.IOException; import java.io.PrintWriter; import java.net.URLEncoder;import static jdbc_demo.jdbc.compare;@WebServlet("/Myservlet") //注解配置servlet-patten:/Myservlet web.xml無需再配置; public class Myservlet extends HttpServlet {@Overrideprotected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {}@Overrideprotected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {//設置編碼格式response.setCharacterEncoding("utf-8");//獲取請求的數據String username = request.getParameter("username");String password = request.getParameter("password");/** state=1,則輸入的用戶名與密碼與數據庫中的數據匹配* 登陸成功后的操作* */if (jdbc.compare(username,password)==1){ // PrintWriter out = response.getWriter(); // out.write("<html>"); // out.write("<head>"); // out.write("<meta charset='UTF-8'>"); // out.write("<title>成功</title>"); // out.write("<script>alert('登錄成功')</script>"); // out.write("<style> .body{" + // " background-image: url('1.jpg');n" + // " background-position: center,center;" + // " background-repeat: no-repeat;" + // " background-attachment: fixed;" + // " background-size: cover;" + // "}</style>"); // out.write("</head>"); // out.write("<body class='body'>"); // out.write("成功"); // out.write("</body>"); // out.write("</html>");// 也可以重定向到Suee.jsp的網頁request.getRequestDispatcher("Suee.jsp").forward(request,response);}else{response.setCharacterEncoding("utf-8");PrintWriter out = response.getWriter();out.write("<html>");out.write("<head>");out.write("<meta charset='UTF-8'>");out.write("<title>失敗</title>");out.write("<script>alert('登錄失敗');window.location.assign('http://localhost/login_demo5/')</script>");out.write("</head>");out.write("<body>");out.write("</body>");out.write("</html>");//request.getRequestDispatcher("index.jsp").forward(request,response);}} }jdbc類:連接數據庫與判斷客戶端的信息與數據庫的信息是否匹配
package jdbc_demo;import java.sql.Connection; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement;public class jdbc { //jdbc工具類public static int compare(String account,String password){int state=0; //定義一個state變量來判斷用戶名與密碼是否匹配my m=new my(); //封裝my類Connection con=m.getCon(); //獲取my類下的getCon()方法Statement stmt= null;try {stmt = con.createStatement();} catch (SQLException throwables) {throwables.printStackTrace();}ResultSet resultSet= null;try {resultSet = stmt.executeQuery("select*from student");while(resultSet.next()){if (account.equals(resultSet.getString("username"))&&password.equals(resultSet.getString("password"))){state=1;}System.out.println(state);}} catch (SQLException throwables) {throwables.printStackTrace();}return state;} }首頁:index.jsp--------css+javascript寫的
<%--Created by IntelliJ IDEA.User: 賴波波Date: 2021/6/4Time: 16:25To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head><title>登錄界面</title><script type="text/javascript">function registe() {window.location.href="registe.jsp";}</script><style> .user{/*background:none transparent scroll repeat 0% 0%;*/width: 200px;height: 35px;margin-left: 531px;margin-bottom: 30px;background:transparent;border: white solid 1px;border-radius: 5px;color: white;} .password{color: white;width: 200px;height: 35px;margin-left: 531px;margin-bottom: 30px;background:transparent;border: white solid 1px;border-radius: 5px; } .button{padding-bottom: 22px;border: 1px solid;border-radius: 5px;background-color: rgba(255, 255, 255, 0.5);color: white;width: 80px;height: 20px;margin-left: 526px;margin-bottom: 10px;font-size: 16px; } .h1{margin-bottom: 50px;color: white;margin-top: 110px; } .body{background-image: url("1.png");background-position: center,center;background-repeat: no-repeat;background-attachment: fixed;background-size: cover; } .button1{padding-bottom: 22px;border: 1px solid;border-radius: 5px;background-color: rgba(255, 255, 255, 0.5);color: white;width: 80px;height: 20px;margin-left: 50px;font-size: 16px; }</style> </head> <body class="body"> <h1 align="center" class="h1">登錄</h1><form action="/login_demo5/Myservlet" method="post"> <input type="text" name="username" placeholder="請輸入用戶名" class="user"> <br><input type="password" name="password" placeholder="請輸入密碼" class="password"><br> <input type="submit" vlaue="登錄" align="center" class="button"><input type="button" value="注冊" class="button1" onclick="registe()"> </form> </body> </html>登錄成功界面:
<%--Created by IntelliJ IDEA.User: 賴波波Date: 2021/6/4Time: 18:52To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head><title>成功</title><style>.body{background-image: url("1.jpg");background-position: center,center;background-repeat: no-repeat;background-attachment: fixed;background-size: cover;}.h1{color: rgb(246, 255, 16);}</style> </head> <body class="body"> <%String user = request.getParameter("username"); %> <h1 align="center" class="h1">歡迎<%=user%>登錄</h1></body> </html>注冊界面:
<%--Created by IntelliJ IDEA.User: 賴波波Date: 2021/6/5Time: 21:36To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head><title>注冊</title><style>.user{/*background:none transparent scroll repeat 0% 0%;*/width: 200px;height: 35px;margin-left: 531px;margin-bottom: 30px;background:transparent;border: white solid 1px;border-radius: 5px;color: white;}.password{color: white;width: 200px;height: 35px;margin-left: 531px;margin-bottom: 30px;background:transparent;border: white solid 1px;border-radius: 5px;}.button{border: 1px solid;border-radius: 5px;background-color: rgba(255, 255, 255, 0.25);color: white;width: 210px;height: 40px;margin-left: 526px;margin-bottom: 10px;font-size: 16px;}.h1{margin-bottom: 50px;color: white;margin-top: 110px;}.body{background-image: url("2.png");background-position: center,center;background-repeat: no-repeat;background-attachment: fixed;background-size: cover;}</style> </head> <body class="body"> <h1 align="center" class="h1">注冊</h1><form action="" method="post"><input type="text" name="username" placeholder="請輸入注冊名" class="user"> <br><input type="password" name="password" placeholder="請輸入密碼" class="password"><br><input type="submit" vlaue="提交" align="center" class="button"> </form> </body> </html>?
總結
以上是生活随笔為你收集整理的Servlet+html+css+jsp+mysql实现用户登录的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: delphi查看源码版本_Eigen 版
- 下一篇: 设计模式笔记2-简单工厂模式(带有自制U