Help coderanch get a
new server
by contributing to the fundraiser
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

type "button" vs "submit"

 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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)
 
Sheriff
Posts: 67750
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

What am I doing wrong?



You changed the button type to "button".

If you want a button to have the submit semantics, leave the type as "submit".
 
Michael Waserman
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry for the lock of knowlege. I did not think that "button" does not send the name value to the session (my sewrvlet blows up while String userApp = req.getParameter("userApp"); ).
The problem is when I use type "submit" even if ID and/or password is not entered the form gets submitted to the servlet. 'return false' does not prevent the form from being submitted:

if (err_msg.length > 0){
alert (err_msg);
return false;
} else {
aForm.submit();
}

Is there a way to pass the button identifier to the servlet using type "button"?


Thanks. michael.
 
Bear Bibeault
Sheriff
Posts: 67750
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

The problem is when I use type "submit" even if ID and/or password is not entered the form gets submitted to the servlet.



If this is the problem, changing the button type is not the solution; you need to perform your form vaildation correctly.

Declare you onsubmit handler to return true or false as appropriate, and place it on the form tag as onsubmit="return validateFunction()".

Any further discussion should take place in the HTML/Javascript forum, so moving this topic there.
 
Michael Waserman
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Bear. I played a little bit with the form tag and got it to work.

Thanks again. Mike.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic