1. 程式人生 > >javaweb登錄註冊頁面;

javaweb登錄註冊頁面;

put sub cati 跳轉 edi button word ons 密碼錯誤

初學javaweb,第一個要做的項目就是登陸註冊頁面,被這兩個頁面折磨了兩天,終於寫出來了,相信也會有許多初學者會面臨著寫這個頁面的問題,給大家提供一段代碼,僅供參考。

登錄頁面;

<form action="in.jsp" method="post">
<input type="hidden" name="hidden" value="deng">
<table>
<tr>
<td width="150px" height="50px">
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;賬號
</td>
<td width="300px" height="50px">
<input type="text" placeholder="請輸入賬號" style="height:30px;width:250px;" name="username">
</td>
</tr>
<tr>
<td>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;密碼
</td>
<td>
<input type="password" placeholder="請輸入密碼" style="height:30px;width:250px;" name="password">
</td>
</tr>
</table>
<div id="button">
<input type="submit" value="登錄">
</form>
<a href="zhuce.jsp">去註冊</a>

註冊頁面;

<form action="in.jsp" method="post">
<input type="hidden" name="hidden" value="zhu">
<table>
<tr>
<td width="150px" height="50px">
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;賬號
</td>
<td width="300px" height="50px">
<input type="text" placeholder="請輸入賬號" style="height:30px;width:250px;" name="username">
</td>
</tr>
<tr>
<td>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;密碼
</td>
<td>
<input type="password" placeholder="請輸入密碼" style="height:30px;width:250px;" name="password">
</td>
</tr>
<tr>
<td>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;確認密碼
</td>
<td>
<input type="password" placeholder="請再次輸入密碼" style="height:30px;width:250px;" name="password0">
</td>
</tr>
</table>
<div id="button">
<input type="submit" value="註冊">
</form>

中間需要跳轉的in.jsp;

<%
request.setCharacterEncoding("utf-8");
String hidd=request.getParameter("hidden");
if("deng".equals(hidd)){
String username=request.getParameter("username");
String password =request.getParameter("password");

if(username!=null&&username.trim().length()>0){
Object oo=application.getAttribute(username);
if(oo==null){
response.sendRedirect("java.jsp?code=5");
}
else{
String userinfo=(String)oo;
if(password.equals(userinfo.split(",")[1])){
response.sendRedirect("pinglun.jsp");
}
else{
response.sendRedirect("java.jsp?code=6");
}
}
}
else{
response.sendRedirect("java.jsp?code=4");
}
}
if("zhu".equals(hidd)){
String username=request.getParameter("username");
String password=request.getParameter("password");
String password0=request.getParameter("password0");
Object obj=application.getAttribute("username");
if(obj==null){
if(password.equals(password0)){
application.setAttribute(username, username+","+password);
response.sendRedirect("java.jsp?code=2");
}
else{
response.sendRedirect("java.jsp?code=3");
}
}
else{
response.sendRedirect("java.jsp?code=1");
}
}

%>

java.jsp;

<%
String code=request.getParameter("code");
if(code.equals("1")){
out.print("<h5>該用戶已經存在 </h5>");
}
if(code.equals("2")){
out.print("<h5>註冊成功 </h5>");
}
if(code.equals("3")){
out.print("<h5>兩次輸入密碼不同 </h5>");
}
if(code.equals("4")){
out.print("<h5>請輸入用戶名 </h5>");
}
if(code.equals("5")){
out.print("<h5>請先前往註冊 </h5>");
}
if(code.equals("6")){
out.print("<h5>密碼錯誤! </h5>");
}

%>
<a href="denglu.jsp">登錄</a>
<a href="zhuce.jsp">註冊</a>

javaweb登錄註冊頁面;