Servlet作业--实现注册和登录
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了Servlet作业--实现注册和登录,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含5089字,纯文字阅读大概需要8分钟。
内容图文
1.注册页面 zhuce.html
<! DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd" > < html > < head > < meta http-equiv ="Content-Type" content ="text/html; charset=UTF-8" > < title >注册新用户</title><script type="text/javascript">function check() { if(zhuce.userid.value ==null|| zhuce.userid.value.trim().length ==0) { alert("请输入用户代码") returnfalse; } if(zhuce.username.value ==null|| zhuce.username.value.trim().length ==0) { alert("请输入用户名称"); returnfalse; } if(zhuce.password.value ==null|| zhuce.password.value.trim().length ==0) { alert("请输入密码"); returnfalse; } if(zhuce.password2.value ==null|| zhuce.password2.value.trim().length ==0) { alert("请再次密码"); returnfalse; } if(zhuce.password2.value != zhuce.password.value) { alert("两次输入的密码不一致"); returnfalse; } returntrue; } </script></head><body><form id="zhuce" action="zhuce" onSubmit="return check()"> 用户代码:<input id="userid" type="text" name="userid" width=30 /><br><br> 用户名称:<input id="username" type="text" name="username" width=30 /><br><br> 输入密码:<input id="password" type="password" name="password" width=30 /><br><br> 确认密码:<input id="password2" type="password" name="password2" width=30 /><br><br><input type="submit" value="提交"/></form></body></html>
2.注册处理 zhuce.java
package com.hanqi; import java.io.IOException; import javax.servlet.ServletContext; import javax.servlet.ServletException; import javax.servlet.ServletRequest; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.catalina.User; import com.hanqi.*; /** * Servlet implementation class zhuce */ @SuppressWarnings("unused") publicclass zhuce extends HttpServlet { privatestaticfinallong serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */public zhuce() { super(); // TODO Auto-generated constructor stub } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */protectedvoid doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//使提示页面显示中文 response.setCharacterEncoding("GBK"); String un = new String(request.getParameter("username").getBytes("ISO-8859-1"),"UTF-8"); String ui = request.getParameter("userid"); String pw = request.getParameter("password"); String pw2 = request.getParameter("password2"); if(ui == null || ui.trim().length() == 0) { response.getWriter().append("用户代码不能为空");
//自动跳转到注册页面 response.setHeader("refresh", "3;URL=zhuce.html"); } elseif(un == null || un.trim().length() == 0) { response.getWriter().append("用户名不能为空"); response.setHeader("refresh", "3;URL=zhuce.html"); } elseif(pw == null || pw.trim().length() == 0) { response.getWriter().append("密码不能为空"); response.setHeader("refresh", "3;URL=zhuce.html"); } else { ServletContext application = this.getServletContext(); Object obj = application.getAttribute(ui); if(obj != null) { response.getWriter().append("用户代码已存在"); response.setHeader("refresh", "3;URL=zhuce.html"); } else { //以对象的形式保存用户信息 //实体类,对用户信息的封装 user zc = new user(); zc.setuserID(ui); zc.setuserName(un); zc.setpassword(pw); response.getWriter().append("注册成功"); response.setHeader("refresh", "3;URL=index.html"); } } } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */protectedvoid doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub doGet(request, response); } }
3.自定义存储用户注册信息的类: user.java
package com.hanqi; public class user {
//用户代码 private String ui; public String getuserID() { return ui; } public void setuserID(String ui) { this.ui = ui; } //用户名称private String un; public String getuserName() { return un; } publicvoid setuserName(String un) { this.un = un; } //密码private String pw; public String getpassword() { return pw; } publicvoid setpassword(String pw) { this.pw = pw; } }
4.登录页面 index.html
<! DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd" > < html > < head > < meta http-equiv ="Content-Type" content ="text/html; charset=UTF-8" > < title >请登录</title><script type="text/javascript">function check() { var uid = document.getElementById("userid"); if(uid.value =="") { alert("用户代码不能为空") returnfalse; } if(login.password.value =="") { alert("密码不能为空"); returnfalse; } returntrue; } </script></head><body><form id="login" method="post" action="CheckLogin" onSubmit="return check();"> 用户代码: <input id="userid" name="userid" type="text" width="30"/><a href="P142-3.1.jsp">注册新用户</a><br> 登录密码: <input id="password" name="password" type="password" width="30"/><br><input type="submit" value="登录"/></form></body></html>
5.登录处理页面 CheckLogin.java
原文:http://www.cnblogs.com/shadowduke/p/5014897.html
内容总结
以上是互联网集市为您收集整理的Servlet作业--实现注册和登录全部内容,希望文章能够帮你解决Servlet作业--实现注册和登录所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。
来源:【匿名】