//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" οnsubmit="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 +"|";
}
}