永发信息网

Servlet为什么不能检测邮箱格式???

答案:2  悬赏:20  手机版
解决时间 2021-07-18 13:32
  • 提问者网友:杀生予夺
  • 2021-07-17 20:33

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>

最佳答案
  • 五星知识达人网友:孤独入客枕
  • 2021-07-17 20:52

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);
}


这段代码的逻辑出现错误了.


你的email本来就是空了,怎么还可能有 @ 呢? 所以那个条件自然是flase, 所以就会执行else代码

全部回答
  • 1楼网友:山有枢
  • 2021-07-17 21:23

其实这个问题你可以看下servlet 的GET 与POST提交的区别就可以了

我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯