Why is my request.getParameter() gives me NULL?
843835Sep 19 2002 — edited Sep 20 2002Yup, i dont get it, why did i get a null value for this:
String login = request.getParameter("userLogin");
Here are my JSP/Servlets/java files
===================================
import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.*;
public class ControllerServlet extends HttpServlet
{
public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
{
doPost(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
{
System.out.println("\nGot the POST from the Login Page");
String login = request.getParameter("userLogin");
if(login == null)
{
setErrorPage(request, response, "An unknown error occurs!");
return;
}
if(login.equals("userLogin"))
{
System.out.println("Obtained the values from the textboxes in UserLogin.jsp");
String userNRIC = request.getParameter("userNRIC");
String userPassword = request.getParameter("userPassword");
//check with the list of users in the database
if(authenticate(userNRIC, userPassword))
{
System.out.println("Registered User");
createSession(request, userNRIC, userPassword);
forward(request, response, "/JSP/Main.jsp");
}
else
{
System.out.println("Unregistered User");
setErrorPage(request, response, "Wrong user id or password entered");
}
}
else
{
System.out.println("Login is NOT null, but it is not 'userLogin' as well");
//change password
}
return;
}
private boolean authenticate(String nric, String password)
{
System.out.println("In process of validating user");
MyDatabase db = new MyDatabase();
String sql = "SELECT NRIC, password FROM Users" +
" WHERE NRIC = '" + nric + "'" +
" AND Password = '" + password + "'";
try
{
Statement stmt=db.con.createStatement();
ResultSet rs = stmt.executeQuery(sql);
if(rs.next())
{
System.out.println("This is a registered player");
rs.close();
stmt.close();
db.con.close();
return true;
}
System.out.println("No such player existed in DB");
rs.close();
stmt.close();
db.con.close();
}catch(SQLException sqle) {
System.out.println(sqle.toString());
}
return false;
}
private void createSession(HttpServletRequest request, String nric, String password)
{
System.out.println("Creating session for this user");
HttpSession session = request.getSession(true);
String userID = request.getParameter("userName");
User user = new User(nric, password);
//user.getUserDetail();
session.setAttribute("user", user);
}
private void forward(HttpServletRequest request, HttpServletResponse response, String url) throws ServletException, IOException
{
request.getRequestDispatcher(url).forward(request, response);
}
private void setErrorPage(HttpServletRequest request, HttpServletResponse response, String s) throws ServletException, IOException
{
System.out.println("Some unknown error occurs");
request.setAttribute("ErrorText", s);
request.setAttribute("ErrorURL", "/JSP/UserLogin.jsp");
forward(request, response, "/JSP/ErrorPg.jsp");
}
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
import java.io.*;
import java.sql.*;
import java.util.*;
public class User
{
public String nric;
public String name;
public String password;
public String email;
public String score;
public Vector userList_v = new Vector();
public User()
{
}
public User(String snric, String spassword)
{
nric = snric;
password = spassword;
}
public void getUserDetail()
{
MyDatabase db = new MyDatabase();
String sql = "SELECT * FROM Users" +
" WHERE NRIC = '" + nric + "'" +
" AND Password = '" + password + "'";
try
{
Statement stmt=db.con.createStatement();
ResultSet rs = stmt.executeQuery(sql);
if(rs.next())
{
email = rs.getString("Email");
name = rs.getString("Name");
score = rs.getString("Score");
rs.close();
stmt.close();
db.con.close();
}
rs.close();
stmt.close();
db.con.close();
}catch(SQLException sqle) {
System.out.println(sqle.toString());
}
}
//If intended to get all users
public void getUserDetail(String sql)
{
MyDatabase db = new MyDatabase();
try
{ sql = sql.trim();
Statement stmt=db.con.createStatement();
ResultSet rs = stmt.executeQuery(sql);
if(rs.next()){
do{
User user_obj = new User();
user_obj.nric = rs.getString("NRIC");
user_obj.password = rs.getString("Password");
user_obj.name = rs.getString("Name");
user_obj.email = rs.getString("Email");
user_obj.score = rs.getString("Score");
userList_v.addElement(user_obj);
}while(rs.next());
}
else{
System.out.println("No data to be retrieved");
db.con.close();
}
}catch(SQLException sqle) {
System.out.println(sqle.toString());
}
}
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
ErrorPg.jsp
<HTML>
<HEAD>
<TITLE>Error Page</TITLE>
</HEAD>
<BODY>
You have encountered an Error! The error message is as
follow:<BR>
<%= request.getAttribute("ErrorText") %>
<BR>
Click <a href="<%= request.getAttribute("ErrorURL") %>">here</a> to return.
</BODY>
</HTML>
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
UserLogin.jsp
<HTML>
<HEAD>
<TITLE>DisplayQuestions</TITLE>
<HEAD>
<BODY>
<FORM
ACTION="servlet/ControllerServlet"
METHOD="post">
User NRIC: <INPUT TYPE="text" NAME="userNRIC" SIZE="20" MAXLENGTH="20"><BR>
Password: <INPUT TYPE="password" NAME="userPassword" SIZE="20" MAXLENGTH="20"><BR>
<INPUT TYPE="submit" VALUE="Login">
</FORM>
<INPUT TYPE="hidden" NAME="userLogin" VALUE="Login">
</BODY>
</HTML>
`````````````````````````````````````````````````
Right, some of the lines are not alined well, due
to the fact that the textbox provided is quite limit
in width.
The problem i got was, the login is always NULL.
Another problem arises, when i tried to forward from a JSP to a servlet and then to the JSP, that is from my ErrorPg.jsp back to the ControllerServlet, and then to my UserLogin.jsp.
I have the error message like this:
/servlet/JSP/UserLogin.jsp
But the problem is, it shouldnt have the "/servlet" at all, since i did not specify it in the first place.
Please reply asap.