Can anybody help me with the following problem?
I have a JSP, which is the first page to be displayed; the JSP should create
a ValidateBean Object.
When the submit button is pressed the servlet FormValidate is called to do
some validation processing. This mechanism all works ok but when I try to
access the ValidateVBean object in the servlet I get a NULL POINTER
EXCEPTION error because it cannot see the bean created in the JSP. I have
checked that the JSP and server are accessing the same session and they are.
JSP (Simplified).
<html>
<head>
<%@page language="java" import="bean.ValidateBean" %>
<jsp:useBean id="validate" class="bean.ValidateBean" scope="session"/>
<FORM name="PersonalDetails" action=/servlet/FormValidate method=post>
<% session.putValue("validate", validate); %>
<table width="600" border="1" cellspacing="0" cellpadding="0"
bgcolor="#FF9933" bordercolor="#333399" align="center">
<tr>
<td height="20" align="left" valign="middle" colspan="6"
class="form"><font face="Arial," size="1" class="form">First
Name </font></td>
<td height="20" align="left" valign="top" colspan="9"
class="form"><font face="Arial," size="1">
<input type="text" name="firstname" size="30">
</font></td>
<td height="20" align="left" valign="left" colspan="6"
class="form"><font face="Arial," size="1" class="form">
<% if (validate.isFirstNameValid() == true) { %>
First Name Must Be Entered
<% } %>
<tr>
<td height="34" colspan="18" class="form">
<div align="center"> <font size="1">
<input type="submit" name="Submit" value="Submit">
</font></div>
</td>
</tr>
</table>
</FORM>
</HEAD>
</HTML>
Java Bean Class.
package bean;
public class ValidateBean {
private boolean firstNameValid = true;
public boolean isFirstNameValid() {
return firstNameValid;
}
public boolean validateFirstName(String inpFirstName) {
if (inpFirstName == " ") {
firstNameValid = false;
return false;
}
else {
firstNameValid = true;
return true;
}
}
}
Processing Servlet.
package servlets;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class FormValidate extends HttpServlet {
bean.ValidateBean validateBean;
public void doGet (HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException
{
doPost(req, res);
}
public void doPost(HttpServletRequest req, HttpServletResponse res) throws
ServletException, IOException {
//
try {
HttpSession sess = req.getSession();
System.out.println(sess.getId());
validateBean = (bean.ValidateBean)sess.getValue("validate");
if (validateBean.validateFirstName(req.getParameter("firstname")))
valid = true;
else
valid = false;
--- Other Stuff --
===========================================================================
To unsubscribe: mailto [EMAIL PROTECTED] with body: "signoff JSP-INTEREST".
Some relevant FAQs on JSP/Servlets can be found at:
http://java.sun.com/products/jsp/faq.html
http://www.esperanto.org.nz/jsp/jspfaq.html
http://www.jguru.com/jguru/faq/faqpage.jsp?name=JSP
http://www.jguru.com/jguru/faq/faqpage.jsp?name=Servlets