本文目錄一覽:
求大神寫一下jsp的簡單的註冊界面代碼。
1.需要一個jsp頁面:
//login.jsp核心代碼:
form action=”${pageContext.request.contextPath}/servlet/UserServlet” method=”post”
input type=”text” name=”loginname” /input type=”password” name=”password”/
input type=”submit” value=”登錄”/
/form
2.需要一個servlet來驗證登錄信息
//UserServlet 核心代碼
class UserServlet extends HttpServlet{
protected void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
process(request, response);
}
protected void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
process(request, response);
}
private void process(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
PrintWriter pw = response.getWriter();
request.setCharacterEncoding(“UTF-8”);
response.setContentType(“text/html”);
String loginname = request.getParameter(“loginname”);
String password = request.getParameter(“password”);
//創建一個service來處理業務邏輯(包括查詢數據庫操作)
UserService service = new UserService();
boolean bool = service.validateUser(loginname,password);
if(!bool){
pw.println(“用戶名或密碼錯誤”);
}else{
pw.println(“登錄成功”);
}
}
3.需要一個service處理業務邏輯(包括查詢數據庫操作)
//UserService 核心代碼
public class UserService{
/**
*查詢數據庫驗證用戶是否存在,返回boolean
*/
public boolean validateUser(String loginname,String password){
boolean bool = false;
Connection conn = null;
PreparedStatement ps = null;
//這裡以mysql為例
try {
Class.forName(“com.mysql.jdbc.Driver”).newInstance();
conn = DriverManager.getConnection(“jdbc:mysql://localhost:3306/test”, “root”, “”);
String sql = “select login_name,pass_word from t_user where login_name=? and pass_word=?”;
ps = conn.prepareStatement(sql);
ps.setString(0, loginname);
ps.setString(1, password);
ResultSet rs = ps.executeQuery();
if(rs.next()){
bool = true;
}
} catch (Exception e) {
e.printStackTrace();
} finally{
try {
if(conn != null){
conn.close();
conn = null;
}
if(ps != null){
ps.close();
ps = null;
}
} catch (SQLException e) {
e.printStackTrace();
}
}
return bool;
}
}
JSP編寫一個登陸界面
1、首先準備Dreamweaver8軟件,解壓安裝。如下圖所示:這件點擊安裝程序,然後輸入序列號就可以了。
2、在安裝軟件時候,我們可以看到是否關聯【jsp文件】。
3、安裝好了軟件以後,我們打開Dreamweaver8軟件。點擊菜單上的【文件】——【新建】。
4、彈出【新建文檔】——【動態頁】——【jsp】——【創建】。
5、點擊【拆分】,在【body】標籤下面輸入:% out.println(“Hello World!”); %。
6、然後按快捷鍵【ctrl+s】保存jsp文件。保存類型jps;。
jsp登錄代碼
login.jsp
%@ page language=”java” pageEncoding=”utf-8″%
html
head
title登錄/title
script language=”javascript”
function checkform() {
if (form4.name.value.length==0){
alert(“請輸入正確的姓名!”);
form4.name.focus();
return false;
}
if (form4.password.value.length==0){
alert(“請輸入正確的密碼!”);
form4.password.focus();
return false;
}
}
/script
style type=”text/css”
!–
#Layer1 {
position:absolute;
left:16px;
top:442px;
width:276px;
height:239px;
z-index:1;
}
#Layer2 {
position:absolute;
left:29px;
top:305px;
width:181px;
height:137px;
z-index:2;
}
—
/style
/head
body
div id=”Layer1″ style=”left: 16px; width: 369px;”
form id=”form4″ name=”form4″ method=”post” action=”loginControl.jsp”
table border=”0″ style=”top: 443px; left: 17px; width: 240px; height: 240px;”
tr
td colspan=”3″ div align=”center”strong用戶登錄/strong/div/td
/tr
tr
td div align=”right”strong用戶名:/strong/div/td
td colspan=”1″input type=”text” name=”name” //td
/tr
tr
td div align=”right”strong密 碼:/strongstrong /strong/div/td
td colspan=”1″input type=”password” name=”password” //td
/tr
tr
td height=”37″ colspan=”3″
label
input type=”submit” name=”Submit” value=”登錄”onclick=”javascript:return(checkform());” /
/label
input type=”reset” name=”reset” value=”重填” /
/td
/tr
/table
/form
/div
div id=”Layer2″ img src=2.jpg width=”120″ height=”120″//div
label/label
/body
/html
loginControl.jsp
%@ page language=”java” import=”java.util.*” pageEncoding=”UTF-8″%
%@ include file=”tdb.jsp”%
!DOCTYPE HTML PUBLIC “-//W3C//DTD HTML 4.01 Transitional//EN”
html
style type=”text/css”
!–
#Layer1 {
position:absolute;
left:9px;
top:363px;
width:218px;
height:133px;
z-index:1;
}
—
/style
head
titleMy JSP ‘loginControl.jsp’ starting page/title
/head
body
%
String name= request.getParameter(“name”);
String password= request.getParameter(“password”);
System.out.println(“成功11111”);
sql=”SELECT * FROM tdb WHERE user_Name='”+name+”‘ AND user_Pwd='”+password+”‘”;
rs = smt.executeQuery(sql);
if(!rs.next())
{%
script type=”text/javascript”
alert(“用戶名和密碼不正確!!!”);
window.location.href=’index.jsp’;
/script%
}
response.sendRedirect(“main.jsp”);
%
/body
/html
tdb.jsp
%
Class.forName(“oracle.jdbc.OracleDriver”);
java.sql.Connection con=null;
con=java.sql.DriverManager.getConnection(“jdbc:oracle:thin:@127.0.0.1:1521:ABC”,”zhan”,”123″);
java.sql.Statement smt=con.createStatement();
java.sql.ResultSet rs;
java.sql.PreparedStatement ps;
String sql;
%
編寫用戶註冊於登錄的JSP頁面的全部程序代碼
3個jsp文件,第一個是login.jsp,第二個是judge.jsp,第三個是afterLogin.jsp
%@ page language=”java” contentType=”text/html; charset=GB18030″
pageEncoding=”GB18030″%
%@ page import=”java.util.*” %
!DOCTYPE HTML PUBLIC “-//W3C//DTD HTML 4.01 Transitional//EN”
html
head
title登錄頁面/title
/head
body
form name=”loginForm” method=”post” action=”judgeUser.jsp”
table
tr
td用戶名:input type=”text” name=”userName” id=”userName”/td
/tr
tr
td密碼:input type=”password” name=”password” id=”password”/td
/tr
tr
tdinput type=”submit” value=”登錄” style=”background-color:pink” input type=”reset” value=”重置” style=”background-color:red”/td
/tr
/table
/form
/body
/html
%@ page language=”java” contentType=”text/html; charset=GB18030″
pageEncoding=”GB18030″%
%@ page import=”java.util.*” %
!DOCTYPE HTML PUBLIC “-//W3C//DTD HTML 4.01 Transitional//EN”
html
head
title身份驗證/title
/head
body
%
request.setCharacterEncoding(“GB18030”);
String name = request.getParameter(“userName”);
String password = request.getParameter(“password”);
if(name.equals(“abc”) password.equals(“123”)) {
%
jsp:forward page=”afterLogin.jsp”
jsp:param name=”userName” value=”%=name%”/
/jsp:forward
%
}
else {
%
jsp:forward page=”login.jsp”/
%
}
%
/body
/html
%@ page language=”java” contentType=”text/html; charset=GB18030″
pageEncoding=”GB18030″%
!DOCTYPE HTML PUBLIC “-//W3C//DTD HTML 4.01 Transitional//EN”
html
head
title登錄成功/title
/head
body
%
request.setCharacterEncoding(“GB18030”);
String name = request.getParameter(“userName”);
out.println(“歡迎你:” + name);
%
/body
/html
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/219906.html