JAVA+JSP的作业题目:
Login.jsp页面: 用表单输入用户名和口令,如果正确(假设正确的用户名是sky, 正确的口令是ocean,),进入select.jsp, 如果错误,提示用户名或口令错误;
效果是当用户输入账号密码点击提交之后在当前的JSP页面实现验证,如果正确就跳转到指定的页面,错误则给出错误信息要求重新输入
<%@ page contentType="text/html; charset=utf-8" language="java" import="java.sql.*" errorPage="" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
</head>
<body>
<form id="form1" name="form1" method="post" action="">
<p>
<label>用户名:
<input type="text" name="User" id="textfield" />
</label>
</p>
<p>
<label>密码:
<input type="password" name="Password" id="textfield2" />
</label>
</p>
<p>
<label>
<input type="submit" name="button" id="button" value="提交" />
</label>
<label>
<input type="reset" name="button2" id="button2" value="重置" />
</label>
</p>
</form>
<%
String name=request.getParameter("name");
String pw=request.getParameter("password");
if(name.equals("sky")&&pw.equals("ocean"))
{
session.setAttribute("name", name);
request.getRequestDispatcher("Select.jsp").forward(request, response);
}
else{
out.write("用户名或密码错误!");
out.println("<br><a href= Login.jsp>返回登录");
}
%>
</body>
</html>
目前个人只用过ajax、servlet、jsp实现过,但在本页面完成实现的方法暂未找到,我是渣渣,对不起了各位
如果是用js验证就非常简单,楼主说的是用java验证,如果满意往采纳
<input type="text" name="username"/>
<input type="password" name="password"/>
<%
String username = request.getParameterByName("username");
String password = request.getParameterByName("password");
if(username != null && username != "" && password != null && password != ""){
if(username.equals("sky") && password.equals("ocean")){
response.sendRedirect("select.jsp");
}else{
//用户名密码错误
}
}else{
//请输入用户名密码
}
%>
就是一个普通的登陆功能而已,采纳答案给你发源码链接!
你是要在jsp写java代码验证吗? 还是说要表单提交到java方法验证, 建议你使用后者 表达提交到对应的方法 然后判断 返回对应的提示就行