注冊登陸界面
1.首先創建login.jsp登陸頁面
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<form action="login" method="post">
姓名: <input type="text" name="name"><br>
密碼: <input type="text" name="pwd"><br>
用戶名:<input type="text" name="userName"><br>
年齡: <input type="text" name="age"><br>
<input type="submit" value="注冊">
<a href="">登錄</a>
</form>
</body>
</html>
在寫form表單的時候有兩個點需要注意,分別是action及method
action:下面的表單擁有兩個輸入欄位以及一個提交按鈕,當提交表單時,表單資料會提交到名為 action="xxxxxx"的頁面
method: 屬性規定瀏覽器使用 method 屬性設定的方法將表單中的資料傳送給服務器進行處理,共有兩種方法:POST 方法和 GET 方法
2.再新建utils包,創建一個公共工具DBUtils類 -----公共工具DBUtils類(此工具類解決代碼冗余問題)
DBUtils簡化了JDBC的開發步驟,使得我們可以用更少量的代碼實作連接資料庫的功能
-
// 1.加載驅動 -
// 2.使用DriverManager建立到資料庫的連接 -
// 7.關閉資源
package utils;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class DBUtils {
// 1.加載驅動
static {
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
// 2.使用DriverManager建立到資料庫的連接
public static Connection getConn() {
Connection conn = null;
try {
conn = DriverManager.getConnection(
"jdbc:sqlserver://localhost:1433;databaseName=school",
"sa", "1");
} catch (SQLException e) {
e.printStackTrace();
}
return conn;
}
// 7.關閉資源
public static void close(Connection conn, PreparedStatement pt, ResultSet rt) {
try {
if (pt != null)
pt.close();
} catch (SQLException e) {
e.printStackTrace();
}
try {
if (rt != null)
rt.close();
} catch (SQLException e) {
e.printStackTrace();
}
try {
if (conn != null)
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
最后就是撰寫LoginServlet代碼如下:
package org.yxd;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import jdbc.utils.DBUtils;
public class login extends HttpServlet {
//登錄頁面
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// 1.設定編碼
request.setCharacterEncoding("UTF-8");
response.setCharacterEncoding("UTF-8");
// 設定回應的格式(型別)
response.setContentType("text/html");
// 2.接收請求中的引數
String name =request.getParameter("name");
String pwd =request.getParameter("pwd");
//jdbc的相關代碼
boolean a = false;
//1.獲取工具類
Connection conn =DBUitl.getConn();
//2.準備sql的代碼
String sql = "SELECT * FROM students WHERE name =? and pwd =?";
// 3.準備一個preparedstatement 物件準備和資料庫做互動
PreparedStatement pt =null;
ResultSet rt=null;
try {
pt=conn.prepareStatement(sql);
pt.setString(1, name);
pt.setString(2, pwd);
rt=pt.executeQuery();
if(rt.next()){
a=true;
}else {
a=false;
}
} catch (SQLException e) {
e.printStackTrace();
}
// 3.發送回應
PrintWriter out = response.getWriter();
if (a) {
out.print("登錄成功");
} else {
out.print("登錄失敗");
}
out.flush();
out.close();
}
}
注冊同理可得
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/140389.html
標籤:其他
上一篇:M3U8在線播放
下一篇:Vue 3.0回應式系統編程概述
