Servlet+JavaBean实现注册和登陆
来源:互联网 发布:js中kjur undefine 编辑:程序博客网 时间:2024/05/29 03:57
//index.jsp
<%--
Document : newjspuser
Created on : 2010-4-3, 11:56:38
Author : Administrator
--%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!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>
</head>
<body>
<h1>欢迎您:</h1>
</body>
</html>
<%--
Document : register
Created on : 2010-4-3, 11:23:56
Author : Administrator
--%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!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>注册UI</title>
<script type="text/javascript">
function check()
{
if(document.forms[0].userName.value=="")
{
alert("用户名不能为空");
document.forms[0].userName.focus();
return false;
}
if(document.forms[0].pwd.value=="")
{
alert("密码不能为空");
document.forms[0].pwd.focus();
return false;
}
}
</script>
</head>
<body>
<center><h1>注册页面</h1>
<form action="registerServlet" method="POST" onsubmit="return check()">
<table>
<tr>
<td>用户名:</td>
<td><input type="text" name="username"/></td>
</tr>
<tr>
<td>密码:</td>
<td><input type="password" name ="password"/></td>
</tr>
<tr>
<td colspan="2"> <input type="submit" value="确认">
<input type="reset" value="重填"></td>
</tr>
</table>
</form>
</center>
</body>
</html>
<%--
Document : succeed
Created on : 2010-4-3, 11:49:52
Author : Administrator
--%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!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>
</head>
<body>
<h1>注册成功</h1>
<a href="index.jsp">返回登录界面 </a>
</body>
</html>
<%--
Document : newjspuser
Created on : 2010-4-3, 11:56:38
Author : Administrator
--%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!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>
</head>
<body>
<h1>欢迎您:</h1>
</body>
</html>
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package Servlet;
import bean.User;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
/**
*
* @author Administrator
*/
public class LoginServlet extends HttpServlet {
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=GBK");
PrintWriter out = response.getWriter();
String name = request.getParameter("userName");
String password = request.getParameter("pwd");
String path =this.getServletContext().getRealPath("/db.txt");
InputStream is =this.getServletContext().getResourceAsStream("/db.txt");
StringBuilder sb=new StringBuilder();
int data;
byte[] b=new byte[1024];
while((data=is.read(b))!=-1)
{
String str=new String(b,0,data);
sb.append(str);
}
String[] user1=sb.toString().split("//|");
String temp=name+":"+password;
for(int i=0;i<user1.length;i++){
if(user1[i].equals(temp)){
out.println("用户存在");
response.sendRedirect("user.jsp");
break;
}
}
}
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
/**
* Handles the HTTP <code>POST</code> method.
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
/**
* Returns a short description of the servlet.
* @return a String containing servlet description
*/
@Override
public String getServletInfo() {
return "Short description";
}// </editor-fold>
}
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package Servlet;
import bean.User;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
*
* @author Administrator
*/
public class registerServlet extends HttpServlet {
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
String name = request.getParameter("username");
String password = request.getParameter("password");
String path =this.getServletContext().getRealPath("/db.txt");
InputStream is =this.getServletContext().getResourceAsStream("/db.txt");
OutputStream os = new FileOutputStream(path, true);
User user = new User();
user.setUserName(name);
user.setPwd(password);
os.write(user.toString().getBytes());
os.close();
response.sendRedirect("succeed.jsp");
}
// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
/**
* Handles the HTTP <code>GET</code> method.
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
/**
* Handles the HTTP <code>POST</code> method.
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
/**
* Returns a short description of the servlet.
* @return a String containing servlet description
*/
@Override
public String getServletInfo() {
return "Short description";
}// </editor-fold>
}
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package bean;
/**
*
* @author Administrator
*/
public class User {
private String userName;
private String pwd;
/**
* @return the userName
*/
public String getUserName() {
return userName;
}
/**
* @param userName the userName to set
*/
public void setUserName(String userName) {
this.userName = userName;
}
/**
* @return the pwd
*/
public String getPwd() {
return pwd;
}
/**
* @param pwd the pwd to set
*/
public void setPwd(String pwd) {
this.pwd = pwd;
}
public String toString()
{
return userName + ":" + pwd +"|";
}
}
- Servlet+JavaBean实现注册和登陆
- 方立勋_30天掌握JavaWeb_(Servlet+JSP+JavaBean实现)用户注册和登陆
- jsp+Servlet+javaBean实现登录和注册
- javaWeb入门<1>Servlet+Jsp+JavaBean实现MVC开发模式登陆注册实例详解
- MyBlog部分模块---基于Servlet+JSP+JavaBean实现登陆注册验证功能
- jsp+servlet+JavaBean+MySQL实现登陆实例
- jsp+servlet+mysql+javabean写的登陆注册页面
- Servlet +JSP+Javabean 实现用户登录注册
- 登录和注册(jsp+servlet+JavaBean)
- JSP+JavaBean+Servlet技术实现某网站用户注册和登录功能
- JSP+Servlet+JavaBean实现用户登陆,用户退出
- 登陆实例(JSP+Servlet+JavaBean)
- jsp+servlet+javabean登陆问题
- 用Jsp+Servlet+JavaBean写的注册登陆界面,解决中文乱码
- JSP_实现Servlet登陆,注册,链接Mysql数据库。
- jsp+servlet实现最基本的注册登陆功能
- jsp+servlet实现最基本的注册登陆功能
- jsp+servlet实现最基本的注册登陆功能
- 一点感慨
- 一些思考,不算技术的
- C#中对对象的排序重写IComparer接口
- 暗夜
- 去南京
- Servlet+JavaBean实现注册和登陆
- 大学应该怎样过?
- Struts1和Struts2的区别
- JSTL 标签
- 揭秘EL表达式语言的基础总结-JSP编程教程
- 【C1】基本学习【S2】Making Connections
- 一年的清明时节
- 发一个MFC类库详解
- arm内核移植过程