我创建了一个包含三个字段Usertype,Username和Password的Login页面。 Usertype有选择框(或选择标记),Username有textfield,Password也有textfield。登录每个字段必须正确。问题是我成功地能够验证用户名和密码但无法使用usertype,用户名和密码。我有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>
当我运行此程序并使用正确的值验证它时,它也显示登录失败/失败页面。 请解决此问题
答案 0 :(得分:0)
在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>
答案 1 :(得分:0)
@Atiq:您上面提到的代码,似乎您没有在jsp中将name
标记的<select>
属性设置为usertype
。
尝试添加它,然后检查,它应该工作。