jsp 登陆验证

huoyunka 2010-02-05 04:27:26
main.jsp 是登陆页面
<form method="post" action="check.jsp">

<input type="text" name="id"/>
<input type="password" name="password"/>
<input type="submit" name=“提交"/>
</form>



当我点击提交按钮的时候, 在check.jsp 做检测:
问题是当id和password为空或者是错误的时候, 跳到main。jsp并给出提示信息
请问这个怎么做, 能给出具体的代码就更好了,先谢谢了@
...全文
562 24 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
24 条回复
切换为时间正序
请发表友善的回复…
发表回复
jackyfedere 2010-02-06
  • 打赏
  • 举报
回复
因为你是用submit的,所以是自动调用你要调用的js页面的,只要在你调用的js页面进行判断,如果可以的话,就return true;否则就return false; ,最后在action那写上onSubmit="return kk()"
crazysox 2010-02-06
  • 打赏
  • 举报
回复
[Quote=引用 20 楼 crazylaa 的回复:]
引用 4 楼 zl3450341 的回复:
HTML code <scripttype="text/javascript">function check(){if(document.form1.id.value==""){
            alert("ID不能为空!");returnfalse;
        }if(document.form1.password.value==""){
            alert("密码不能为空!");returnfalse;
        }returntrue;
} </script> <formname="form1" method="post" action="check.jsp"> <inputtype="text" name="id"/> <inputtype="password" name="password"/> <inputtype="submit" onclick="return check()" name="提交"/> </form>
就这样?

支持
[/Quote]
支持
njlywy 2010-02-06
  • 打赏
  • 举报
回复

<script type="text/javascript">
function check(){
if(document.form1.id.value==""){
alert("ID不能为空!");
return false;
}
if(document.form1.password.value==""){
alert("密码不能为空!");
return false;
}
return true;
}
</script>


<form name="form1" method="post" action="check.jsp">

<input type="text" name="id"/>
<input type="password" name="password"/>
<input type="submit" onclick="return check()" name="提交"/>
</form>

fier325 2010-02-06
  • 打赏
  • 举报
回复
check.jsp:
<%
String id = request.getParameter("id");
String pwd = request.getParameter("password");
String sig=null; //标识变量
if(/*这里验证id和pwd吧 */){
%>
<jsp:forward page="success.jsp"/> //验证成功就跳转到登录成功页面
<%
}else{
%>
sig="failed";
request.setAttribute("sig",sig);
<jsp:forward page="main.jsp"/> //验证失败就跳转到main.jsp
<%
}
%>
main.jsp:
<%
String sig=null;
sig=request.getAttribute("sig"); //获取标识变量
%>
<form method="post" action="check.jsp">

<input type="text" name="id"/>
<%
if(sig==null)
{
%>
<span>id不能为空!</span>
<%
}
%>
<input type="password" name="password"/>
<%
if(sig==null)
{
%>
<span>password不能为空!</span>
<%
}
%>
<input type="submit" name=“提交"/>
</form>

ccycxy123 2010-02-06
  • 打赏
  • 举报
回复
LZ,我觉得要好点的话,首先在你的main.jsp页面来写JS代码,判断他是否为空(代码前面他们都写了),然后,再写一个JSP页面,来判断用户名和密码对不对,如果不对就用forward跳到main.jsp中。这样就完成了简单的登录,如果用servlet或struts2做也行。
crazylaa 2010-02-06
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 zl3450341 的回复:]
HTML code<scripttype="text/javascript">function check(){if(document.form1.id.value==""){
alert("ID不能为空!");returnfalse;
}if(document.form1.password.value==""){
alert("密码不能为空!");returnfalse;
}returntrue;
}</script><formname="form1" method="post" action="check.jsp"><inputtype="text" name="id"/><inputtype="password" name="password"/><inputtype="submit" onclick="return check()" name="提交"/></form>
就这样?
[/Quote]
支持
greatmind829 2010-02-05
  • 打赏
  • 举报
回复
写一个JS 就行了。
wl_ldy 2010-02-05
  • 打赏
  • 举报
回复
哦 人多就是力量大啊 方法很多啊 ,只要能实现就OK了。。。
JavaAlpha 2010-02-05
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 zl3450341 的回复:]
HTML code<scripttype="text/javascript">function check(){if(document.form1.id.value==""){
alert("ID不能为空!");returnfalse;
}if(document.form1.password.value==""){
alert("密码不能为空!");returnfalse;
}returntrue;
}</script><formname="form1" method="post" action="check.jsp"><inputtype="text" name="id"/><inputtype="password" name="password"/><inputtype="submit" onclick="return check()" name="提交"/></form>
就这样?
[/Quote]
[Quote=引用 8 楼 zhaining522 的回复:]
main.jsp 是登陆页面
<form method="post" action="check.jsp">
    <input type="text" name="error" value=" <%=request.getParameter("errorInfo")%>"/>
    <input type="text" name="id"/>
    <input type="password" name="password"/>
    <input type="submit" name=“提交"/>
</form>

check.jsp 验证页面
<%
String id = (String)request.getParameter("id ");
String password= (String)request.getParameter("password");

if(id == null || "".equals(id)){
request.setAttribute("errorInfo","用户名不能为空");
}

if(password== null || "".equals(password)){
request.setAttribute("errorInfo","密码不能为空");
}

actionMapping.findForward("main.jsp");

%>
[/Quote]

这样验证 才比较完美
yancheng1031143057 2010-02-05
  • 打赏
  • 举报
回复
先使用js进行客户端的验证数据的有效性在去跳转到验证登录页面 这样即减轻服务器的压力又保证数据的有效性
aa870816 2010-02-05
  • 打赏
  • 举报
回复
在Action中判断啊,不要把哪些判断语句写到JSP页面里,那样代码会很混乱的!
huangminyanghe 2010-02-05
  • 打赏
  • 举报
回复
一个.jsp文件就够了,你可以先用JavaScript进行空值的验证,然后在开头的时候用
request.getparameter获取两个对象,然后如果对象不为空的时候就调用后台的登录方法,取得返回值然后再判断,如果成功你可以跳转到成功的页面等,总之多写写。其实不是你想向中的困难。
teemai 2010-02-05
  • 打赏
  • 举报
回复
JavaScript验证,4楼正解。

如果非要到check.jsp中验证:
check.jsp

<%
String id = request.getParameter("id");
String pwd = request.getParameter("password");
if(/*这里验证id和pwd吧 */){
%>
<jsp:forward page="success.jsp"/> //验证成功就跳转到登录成功页面
<%
}else{
%>
<jsp:forward page="main.jsp"/> //验证失败就跳转到main.jsp
<%
}
%>
colin_pxx 2010-02-05
  • 打赏
  • 举报
回复
那你可以先在main.jsp页面过滤啊 判断它们是不是为空或者错误然后再让它是否跳入check.jsp
txq1989620 2010-02-05
  • 打赏
  • 举报
回复
在check.jsp中判断 然后forward到mian.jsp即可
Bleibo 2010-02-05
  • 打赏
  • 举报
回复
难道不传值吗,用struts1如何..........

public ActionForward userLogin(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
LoginForm loginform=(LoginForm)form;
ActionMessages errors=new ActionMessages();
String username=loginform.getUsername();
String userpassword=loginform.getPswd();
boolean mark=true;
if(username==null||username.equals("")){
errors.add("noname",new ActionMessage("szscz.no.username.error"));
mark=false;
}
if(userpassword==null||userpassword.equals("")){
errors.add("nopswd",new ActionMessage("szscz.no.userpassword.error"));
mark=false;
}
if(!mark){
saveErrors(request,errors);
return mapping.findForward("LoginFailure");
}
else{
int flag = userDAO.checkUser(loginform);
System.out.print("验证结果ret的值:" + flag);
if (flag == 1) {
HttpSession session=request.getSession();
session.setAttribute("user", loginform.getUsername());
return mapping.findForward("LoginSuccess");
} else {
errors.add("logonR",new ActionMessage("szscz.name.or.password.wrong.error"));
saveErrors(request,errors);
return mapping.findForward("LoginFailure");
}

}
}
zhaining522 2010-02-05
  • 打赏
  • 举报
回复
main.jsp 是登陆页面
<form method="post" action="check.jsp">
<input type="text" name="error" value="<%=request.getParameter("errorInfo")%>"/>
<input type="text" name="id"/>
<input type="password" name="password"/>
<input type="submit" name=“提交"/>
</form>

check.jsp 验证页面
<%
String id = (String)request.getParameter("id ");
String password= (String)request.getParameter("password");

if(id == null || "".equals(id)){
request.setAttribute("errorInfo","用户名不能为空");
}

if(password== null || "".equals(password)){
request.setAttribute("errorInfo","密码不能为空");
}

actionMapping.findForward("main.jsp");

%>
d_star 2010-02-05
  • 打赏
  • 举报
回复
直接JS嘛
wenziaiqiner 2010-02-05
  • 打赏
  • 举报
回复
楼主用JAVASCRIPT先判断是否为空,什么的,把非法输入都过滤掉,
然后跳转到另一个页面做用户密码验证。
蛋黄车 2010-02-05
  • 打赏
  • 举报
回复
看LZ这意思是想要把所有代码全写到一个页面里呀!

估计没有人会写出这样的代码来,并且珍藏到现在的。。。
加载更多回复(4)

67,550

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧