初学javaweb,第一个要做的项目就是登陆注册页面,被这两个页面折磨了两天,终于写出来了,相信也会有许多初学者会面临着写这个页面的问题,给大家提供一段代码,仅供参考。

登录页面;

<form action="in.jsp" method="post">
	<input type="hidden" name="hidden" value="deng">
	<table>
		<tr>
			<td width="150px" height="50px">
				           
				       账号
			</td>
			<td width="300px" height="50px">
				<input type="text" placeholder="请输入账号" style="height:30px;width:250px;" name="username">
			</td>
		</tr>
		<tr>
			<td>
				           
				       密码
			</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">
				           
				       账号
			</td>
			<td width="300px" height="50px">
				<input type="text" placeholder="请输入账号" style="height:30px;width:250px;" name="username">
			</td>
		</tr>
		<tr>
			<td>
				           
				       密码
			</td>
			<td>
				<input type="password" placeholder="请输入密码" style="height:30px;width:250px;" name="password">
			</td>
		</tr>
		<tr>
			<td>
				           
				     确认密码
			</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>