import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class LoginServlet extends HttpServlet
{
Hashtable hashtable = new Hashtable();
public void init(ServletConfig config) throws ServletException
{
super.init(config);
}
public synchronized void doPost(HttpServletRequest request,HttpServletResponse response) throws IOException,ServletException
{
PrintWriter out = response.getWriter();
response.setContentType("text/html;charset=GB2312");
out.print("<HTML><BODY topmargin=100>");
//获取提交的信息
String person_name = request.getParameter("name");
String found_name = null;
if(person_name == null)
{
person_name = "";
}
//从散列表中查找是否已经存在名字
found_name = (String)hashtable.get(person_name);
if(found_name == null)
{
String person_email = request.getParameter("email");
if(person_email == null)
{
person_email = "";
}
StringTokenizer fenxi = new StringTokenizer(person_email," @");
int n = fenxi.countTokens();
if(n >= 3)
{
out.print("<BR>there are exsits illegal letters in you email!");
}
else
{
hashtable.put(person_name,person_name);
out.print("<BR>" + "Login success!");
out.print("<BR>" + "your name is " + person_name);
}
}
else
{
out.print("<BR>" + "This name is exist!");
}
out.print("</BODY></HTML>");
}
public synchronized void doGet(HttpServletRequest request,HttpServletResponse response) throws IOException,ServletException
{
doPost(request,response);
}
}
jsp
<%@ page contentType="text/html;charset=GB2312"%>
<HTML>
<META name="keywords" content="login,submit"></META>
<META name="description" content="login"></META>
<BODY topmargin=50>
<Table align=center>
<TR>
<TD>
<FORM action="servlet/LoginServlet" method=post>
用户名:
<Input type=text name="name">
<BR>
邮箱:
<Input type=text name="email">
<BR>
<Input type="submit" name="submit" value="提交">
</FORM>
</TD>
</TR>
</Table>