[英]multi user login validation unsuccessful using servlet and jsp in eclipse
我创建了一个登录页面,其中包含三个字段:用户类型,用户名和密码。 用户类型具有选择框(或选择标签),用户名具有文本字段,密码也具有文本字段。 要登录,每个字段都必须正确。 问题是我可以成功验证用户名和密码,但是不能处理用户类型,用户名和密码。 我有login.java和index.jsp。
Login.java
package roseindia.net;
import java.io.*;
//import java.util.*;
import javax.servlet.*;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.sql.*;
/*** Servlet implementation class Login
***/
@WebServlet(description = "Login Servlet", urlPatterns = { "/login" })
public class Login extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public Login() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see Servlet#init(ServletConfig)
*/
public void init(ServletConfig config) throws ServletException {
// TODO Auto-generated method stub
}
/**
* @see HttpServlet#service(HttpServletRequest request, HttpServletResponse response)
*/
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
System.out.println("We are service method of servlet");
/*
String username="user";
String password="root";
*/
String un=request.getParameter("username");
String pw=request.getParameter("password");
String ut=request.getParameter("usertype");
String msg=" ";
Connection conn=null;
String url="jdbc:mysql://localhost:3306/";
String dbName="userlogindb";
String driver="com.mysql.jdbc.Driver";
String dbUserName="root";
String dbPassword="root";
try{
Class.forName(driver).newInstance();
conn = DriverManager.getConnection(url+dbName,dbUserName,dbPassword);
String strQuery="select * from registerutable where username='" + un + "' and password1='" + pw + "' and usertype='" + ut + "'";
Statement st= conn.createStatement();
ResultSet rs= st.executeQuery(strQuery);
if(rs.next())
{
msg="Hello " + un + "! Your login is successful";
request.setAttribute("msg", msg);
RequestDispatcher req=request.getRequestDispatcher("/successful.jsp");
req.forward(request, response);
} else
{
msg="Hello " + un + "! Your login failed";
request.setAttribute("msg", msg);
RequestDispatcher req=request.getRequestDispatcher("/failed.jsp");
req.forward(request, response);
}
rs.close();
st.close();
}
catch(Exception e)
{
e.printStackTrace();
}
/*
if(un.equals(username) && pw.equals(password))
{
msg="Hello " + un + "! Your login is successful";
}
else
{
msg="Hello " + un + "! Your login failed";
}
*/
response.setContentType("text/html");
PrintWriter out=response.getWriter();
out.println("<font size='6' color=red>" + msg + "</font>");
}
}
index.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!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=ISO-8859-1">
<title>Login Example</title>
</head>
<body>
<form name="loginform" action="login" method="post">
<div style="width:300px;margin:0 auto;">
<p>User Type: <select>
<option value="employee">Employee</option>
<option value="proj-coordinator">Project Co-ordinator</option>
<option value="proj-manager">Project Manager</option>
<option value="admin">Admin</option>
</select>
</p>
<p>User Name: <input type="text" name="username"></p><br>
<p>Password: <input type="password" name="password"></p><br>
<center><input type="submit" value="Login"></center>
</div>
</form>
</body>
</html>
当我运行该程序并使用正确的值对其进行验证时,它还会显示“登录失败/失败”页面。 请解决此问题
如下更改您在jsp中的选择类型
User Type: <select name="usertype">
<option value="employee">Employee</option>
<option value="proj-coordinator">Project Co-ordinator</option>
<option value="proj-manager">Project Manager</option>
<option value="admin">Admin</option>
</select>
@Atiq:您上面放置的代码,似乎您没有在jsp usertype
<select>
标记的name
属性设置为usertype
。 尝试添加此,然后检查,它应该工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.