When I have both buttons in my
JSP defined as submit it calls the serrvlet normally. As soon as I try to use type 'button' the name is not being passed to the
servlet properly. What am I doing wrong?
JSP:
<%@ page language="java" errorPage="/error_page.jsp"%>
<jsp:useBean id="login" scope="request" class="ccsloginweb.CCSLoginInfoBean" />
<HTML>
<HEAD>
<SCRIPT LANGUAGE="JavaScript">
function checkFields(aForm) {
var err_msg = "";
var reg_exp= /\ +/;
var j=0;
for (var i = 0; i< 2; i++) {
if (document.forms[0].elements[i].value.replace (reg_exp, "").length ==0){
err_msg += " Please enter a valid " + document.forms[0].elements[i].name + ". \n";
if (i==0) {
j=j+2;
}
else {
if (i==1) {
j=j+3;
}
}
}
}
if (err_msg.length > 0){
alert (err_msg);
return false;
} else {
aForm.submit();
}
}
</SCRIPT>
</HEAD>
<BODY>
<P><B><jsp:getProperty name="login" property="message" /></font></B></P>
<FORM action="/servlet/CCSLoginServlet" method="POST" target="_self" name="login">
<TABLE>
<TBODY>
<TR>
<TD>
<TT>Username: </TT><INPUT type="text" name="userID" size=8 maxlength=8>
<TT>Password: </TT><INPUT type="password" name="password" size=8 maxlength=8>
</TD>
</TR>
<TR>
<TD>
<INPUT TYPE="button" name="userApp" value="COMPA" onKlick="checkFields(this.form)">
<INPUT type="button" name="userApp" value="COMPB" onKlick="checkFields(this.form)">
</TD>
</TR>
</TBODY>
</TABLE>
</FORM>
</BODY>
</HTML>
Servlet:
import ccsloginweb.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
public class CCSLoginServlet extends HttpServlet {
public void service(HttpServletRequest req, HttpServletResponse res) throws IOException, ServletException {
HttpSession session = req.getSession(true);
CCSLoginInfoBean loginInfo = new CCSLoginInfoBean();
DBLoginConnection validateLogin = new DBLoginConnection();
String userID = req.getParameter("userID");
String password = req.getParameter("password");
System.out.println("here");
String userApp = req.getParameter("userApp");
System.out.println("userApp = " + req.getParameter("userApp").toString());
String message = "";
try {
loginInfo.setUserID(userID.trim().toUpperCase());
loginInfo.setPassword(password.trim());
loginInfo.setUserApp(userApp.trim());
validateLogin.setSession(session);
validateLogin.setRequest(req);
int validInvalid = validateLogin.isValidUser(userID.trim().toUpperCase(), password.trim());
if (!(validInvalid == +0)) {
if ((validInvalid == +1) || (validInvalid == +3)) {
message = "--- Internal application problem. Please call technical support ---";
}
else {
if (validInvalid == +2) {
message = "--- Invalid User ID and/or Password. Please try again ---";
}
}
loginInfo.setMessage(message);
req.setAttribute("login", loginInfo);
RequestDispatcher rd = getServletContext().getRequestDispatcher("/index.jsp");
rd.forward(req, res);
return;
}
else {
session.setAttribute ("login", loginInfo);
if (userApp.equals("COMPA")){
RequestDispatcher rd = getServletContext().getRequestDispatcher("/options.jsp");
rd.forward(req, res);
}
else {
RequestDispatcher rd = getServletContext().getRequestDispatcher("/optionsBud.jsp");
rd.forward(req, res);
}
}
}
catch (Throwable t) {
t.printStackTrace (System.err);
req.setAttribute("javax.servlet.jsp.jspException", t);
RequestDispatcher rd = getServletContext().getRequestDispatcher("/error_page.jsp");
rd.forward(req, res);
}
}
}
Error:
[Sun Jul 11 12:38:51 EDT 2004] ServletExec 4.0 initialized
[Sun Jul 11 12:39:10 EDT 2004] here
[Sun Jul 11 12:39:10 EDT 2004] ServletExec: caught exception - java.lang.NullPointerException
[Sun Jul 11 12:39:10 EDT 2004] java.lang.NullPointerException
[Sun Jul 11 12:39:10 EDT 2004] at CCSLoginServlet.service(CCSLoginServlet.java:16)
[Sun Jul 11 12:39:10 EDT 2004] at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
[Sun Jul 11 12:39:10 EDT 2004] at com.newatlanta.servletexec.ServletExec.CallServletService(ServletExec.java:1582)
[Sun Jul 11 12:39:10 EDT 2004] at com.newatlanta.servletexec.ServletExec.processServletRequest(ServletExec.java:1535)
[Sun Jul 11 12:39:10 EDT 2004] at com.newatlanta.servletexec.ServletExec.standardServletRequest(ServletExec.java:1429)
[Sun Jul 11 12:39:10 EDT 2004] at com.newatlanta.servletexec.ServletExec.ProcessRequest(Compiled Code)
[Sun Jul 11 12:39:10 EDT 2004] at com.newatlanta.servletexec.ServletExec.ProcessRequest(ServletExec.java:995)