Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2074
  • Last Modified:

Response.sendRedirect doesnt work after inserting record successfully ?

Response.sendRedirect does it work with a buffer ?

--------------------------------------------------------------------------------

I tried with the response.sendRedirect() method, but it didnt work. It does insert the record and shows the following error :

A java exception has occurred during the processing of this request.
Error occurred in JSP element starting at line number 2
java.lang.NullPointerException

I am am from ASP background, so what I think is do we need a buffer property for the response object ? If we do, then pls tell me the syntax and should it be first line before even importing classes ?

I have tried every possible thing in the book, by having the sendRedirect method within the try block, also tried using the finally block, but i just feel so helpless. I aslo tried <jsp:forward> tag but it didnt work .

Here is my source code pls. Help

InsertCatBean.java

import java.sql.*;
import java.io.*;


public class InsertCatBean {

private String dbURL = "jdbcdbc:jspsite";
private String dbDriver = "sun.jdbc.odbc.JdbcOdbcDriver";
private Connection dbCon;

String CatName = null;
String Picture = null;
int Child;
int Display;


public InsertCatBean() {
super();
}

public String getCatName() {
return this.CatName;
}


public void setCatName(String cname) {
cname.replaceAll("'","''");
this.CatName = cname;

}

public int getChild() {
return this.Child;
}

public void setChild(String chd) {
if (chd.equals("null"))
{
Child = 0;
}
else
{
Child = 1;

}
}

public String getPicture() {
return this.Picture;
}

public void setPicture(String pic) {
this.Picture = pic;
}

public void setDisplay(String disp) {
if (disp.equals("null"))
{
Display = 0;
}
else
{
Display = 1;
}
}

public int getDisplay() {
return this.Display;
}

public void doInsert() throws ClassNotFoundException, SQLException {

Class.forName(dbDriver);
dbCon = DriverManager.getConnection(dbURL);

String sql = "INSERT INTO Main (CatName,Child,Picture,Display) values (?,?,?,?)";
PreparedStatement ps = dbCon.prepareStatement(sql);
ps.setString(1,this.CatName);
ps.setInt(2,this.Child);
ps.setString(3,this.Picture);
ps.setInt(4,this.Display);
ResultSet rs = ps.executeQuery();
dbCon.close();

}
}

Submitted page :-

<jsp:useBean id="user" class="InsertCatBean" scope="session"/>
<jsp:setProperty name="user" property="*"/>

<%
try
{
user.doInsert();
}

catch (Exception e)
{
System.out.println(e.getMessage());
}

response.sendRedirect("adminhome.jsp");
}
%>



0
Msbathija
Asked:
Msbathija
  • 14
  • 11
  • 3
  • +2
2 Solutions
 
MsbathijaAuthor Commented:
Sorry guys, the extra brace after the response.sendRedirect() method was for my else block. Please ignore that, thanks.
0
 
Nick_72Commented:
>> It does insert the record

Are you really sure about that?
Look at this code:

String sql = "INSERT INTO Main (CatName,Child,Picture,Display) values (?,?,?,?)";
PreparedStatement ps = dbCon.prepareStatement(sql);
ps.setString(1,this.CatName);
ps.setInt(2,this.Child);
ps.setString(3,this.Picture);
ps.setInt(4,this.Display);
ResultSet rs = ps.executeQuery();
dbCon.close();

When you insert rows or make modifcations to data, use exectueUpdate() instead of executeQuery().
int rowsAffected = ps.executeUpdate();

Also, the URL:
private String dbURL = "jdbcdbc:jspsite";
should be
private String dbURL = "jdbc:odbc:jspsite";

but maybe that was just a typo when you wrote it here?

/Nick
0
 
MsbathijaAuthor Commented:
hey nick,

that one was a typing glicth, but yes it does insert a record. the probelem lies with the redirect
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
copyPasteGhostCommented:
try this..

Submitted page :-

<jsp:useBean id="user" class="InsertCatBean" scope="session"/>
<jsp:setProperty name="user" property="*"/>

<%
try
{
user.doInsert();
}

catch (Exception e)
{
System.out.println(e.getMessage());
}
%>
<jsp:forward page="adminhome.jsp" />
<%
}
%>

Ghost
0
 
MsbathijaAuthor Commented:
hey there..

i did try the same thing before and it didnt work... i tried it one more time and still it doesnt
0
 
searlasCommented:
Nothing to do with your problem, but:

public void setCatName(String cname) {
  cname.replaceAll("'","''");
  this.CatName = cname;
}

Should probably be:

public void setCatName(String cname) {
  this.CatName = cname.replaceAll("'","''");
}

and, are you really wanting to do this..:

public void setDisplay(String disp) {
  if (disp.equals("null"))
  {
    Display = 0;
  }
  else
  {
    Display = 1;
  }
}

or should it be something more like:

public void setDisplay(String disp) {
  if (disp == null || disp.equals("") || disp.equals("null"))
  {
    Display = 0;
  }
  else
  {
    Display = 1;
  }
}
0
 
copyPasteGhostCommented:
umm how about this:

<jsp:useBean id="user" class="InsertCatBean" scope="session"/>
<jsp:setProperty name="user" property="*"/>

<%
try
{
user.doInsert();
%>
<jsp:forward page="adminhome.jsp" />
<%
}

catch (Exception e)
{
System.out.println(e.getMessage());
}
%>

Ghost
0
 
MsbathijaAuthor Commented:
nopes it doesnt work still.. after inserting it still remains on admincat.jsp?add=1
0
 
copyPasteGhostCommented:
well then...
post your .jsp page cause you must be doing something else that I'm not noticing or is that all your jsp page is?
and just for fun... do something like this...

<%
try
{
user.doInsert();
%>
<jsp:forward page="adminhome.jsp" />
<%
}

catch (Exception e)
{
out.println("THIS IS IN THE CATCH");
System.out.println(e.getMessage());
}
%>

I'm thinking that the statement :  user.doInsert();
is failing and therefore your <jsp:foward> is not getting a chance to be executed, since it's jumpping to the catch.
Just an idea...
Let me know how that works out,
Ghost
0
 
MsbathijaAuthor Commented:
hey there, it inserts correctly and also displays the "this is catch" statement on the browser. what might be causing the problem ?
0
 
copyPasteGhostCommented:
well ok that's good...
now we know that the reason it's not forwarding is cause that statment is never happening..

what other errors get displayed? try this..


catch (Exception e)
{
out.println("THIS IS IN THE CATCH");
System.out.println(e.getMessage());
e.printStackTrace();
}

then show me the error that you get and we can work from there..
Ghost
0
 
Nick_72Commented:
try to switch to executeUpdate()

then, close your statement object before you close the connection

ps.close();
dbCon.close();

if you insist using executeQuery(), close the ResultSet, then the Statment, and last the Connection.
0
 
MsbathijaAuthor Commented:
hey nick ,

i did what u said still no luck

hey ghost,

it shows errors on my blazix server window something like

Exception thrown in JSP
javax.servlet.ServletException: Exception thrown in JSP
        at desisoft.jsp.PageContext.handlePageException(PageContext.java:297)
        at desisoft_jsp_adminhome_jsp1082186836578._jspService(desisoft_jsp_admi
nhome_jsp1082186836578.java:290)
        at desisoft.server.JspBaseClass.service(JspBaseClass.java:48)
        at desisoft.server.servlets.Servlet.service(Servlet.java:86)
        at desisoft.server.servlets.Servlet.doService(Servlet.java:62)
        at desisoft.server.common.RequestHandler.doForward(RequestHandler.java:1
07)
        at desisoft.server.servlets.Servlet.forward(Servlet.java:94)
        at desisoft.jsp.PageContext.forward(PageContext.java:254)
        at desisoft_jsp_adminaddcat_jsp1082382590328._jspService(desisoft_jsp_ad
minaddcat_jsp1082382590328.java:313)
        at desisoft.server.JspBaseClass.service(JspBaseClass.java:48)
        at desisoft.server.servlets.Servlet.service(Servlet.java:86)
        at desisoft.server.servlets.Servlet.doService(Servlet.java:62)
        at desisoft.server.common.HttpStream.processRequest(HttpStream.java:639)

        at desisoft.server.conn.ServerThread.run(ServerThread.java:335)
0
 
copyPasteGhostCommented:
can you post your entire jsp page please?
Ghost
0
 
MsbathijaAuthor Commented:
<%@ page language = "java" import ="java.sql.*" %>
<% if (request.getParameter("add") == null)
{
%>
<html>
<head>
<script language = "Javascript">
<!--
function checkForm(form)
{
var form = document.theForm
for (var e = 0; e < form.elements.length; e++)
{
var el = form.elements[e];

if (e1.name == 'CatName')
{
if (el.value == '')
{
 alert('Category name is required!');
 el.focus();
 return false;
}
}

}
}
//-->
</script>

<title>Administration --- Add  Main Category Page</title>
<link rel="stylesheet" href="../style/style.css" type="text/css">
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body bgcolor="#FFFFFF" text="#000000" leftmargin="0" marginwidth="0" marginheight="0" topmargin="0" alink="#0000FF" vlink="#0000FF" link="#0000FF">
<table width="760" border="1" align="center" bordercolor="#000066" cellpadding=0 cellspacing=0>
  <tr valign="top">
    <td><img src="../images/logo.gif" width="110" height="74"></td>
</tr>


  <tr valign="top">
    <td width="760" class="look">
<table width=760>
<tr>
<td width=260 rowspan="2" valign="top"><BR>
  <BR>
      <table width="195" cellpadding="5">
        <tr>
          <td class="look"><a href="adminhome.asp">Home</a></td>
        </tr>
        <tr>
          <td class="look"><a href="adminews.asp">News Admin </a></td>
        </tr>
        <tr>
          <td class="look"><a href="adminlogs.asp"> Logs Admin </a> </td>
        </tr>
 
        <tr>
          <td class="look"><a href="adminjobs.asp">Jobs Admin </a></td>
        </tr>
        <tr>
          <td class="look"><a href="adminpwd.asp">Change Password</a> </td>
        </tr>
        <tr>
          <td class="look"><a href="adminlogout.asp">Sign Out</a> </td>
        </tr>
      </table>

</td>
    <td width="500" align="left" valign="top" class="look"><p><strong><br>
       Add Main Category :</strong></p>
      <form action ="adminaddcat.jsp?add=1" method="post" name="theForm">
        <table width="300" border="1" cellpadding="0" cellspacing="0" bordercolor="#2D4E87">
          <tr>
            <td><table width="300" border="0" cellspacing="2" cellpadding="3">



                <tr>
                  <td width="100" valign="middle" class="look"><strong>Category Name</strong></td>
                  <td valign="top"><input name="CatName" type="text"></td>
                </tr>
                <tr>
                  <td width="100" valign="middle" class="look"><strong>Display</strong></td>
                  <td valign="top"><input name="Display" type="checkbox" value="1"></td>
                </tr>
                <tr>
                  <td width="100" valign="middle" class="look"><strong>Child</strong></td>
                  <td valign="top"><input name="Child" type="checkbox" value="1"></td>
                </tr>
                <tr>
                  <td width="100" valign="middle" class="look"><strong>Image Path </strong></td>
                  <td valign="top"><input name="Picture" type="text"></td>
                </tr>
                <tr>
                  <td colspan="2" class="look"><div align="center">
                      <input type="submit" name="Submit" value="Add Category">
                  </div></td>
                </tr>
            </table></td>
          </tr>
        </table>

      </form> </td>
</tr>
<tr>
 <td align="left" valign="top" class="look"> -- <strong>Category Name</strong> should be unique for the Category Name field. <br>
-- <strong>Image Path field</strong> will have the path to the image on the server for the site.<br>
-- <strong>Child Property</strong> indicates if this catgeory will have one or more sub-main categories. IF CHECKED, it implies, that this Main Category will have one or more sub-main categories. IF UNCHECKED, it will imply that there are no divisions within this catgeory. <br>
-- Display Property is used to control the display of this category on the website. IF CHECKED, it will imply that this Category should be visible on the website. IF UNCHECKED, implies that this category will not be DISPLAYED.</td>
</tr>
</table>
</td>
</tr>
</body>
</html>
<%
}
else
{
%>
<jsp:useBean id="user" class="InsertCatBean" scope="session"/>
<jsp:setProperty name="user" property="*"/>

<%
try
{
user.doInsert();
%>
<jsp:forward page="adminhome.jsp"/>
<%
}


catch (Exception e)
{
out.println("THIS IS A CATCH");
System.out.println(e.getMessage());
e.printStackTrace();

}



}

%>


 

0
 
Nick_72Commented:
not sure if it'll help, but try make the path absolute (beginning with a /).

<jsp:forward page="/adminhome.jsp"/>
0
 
copyPasteGhostCommented:
the problem is that that statement is not being executed...
the page is failing on the doInsert()
but you said that the insert does not fail meaning the record does get inserted into the database..
something doesn't make sense...

I think nick said this already but do this..
change your insert to this..


String sql = "INSERT INTO Main (CatName,Child,Picture,Display) values (?,?,?,?)";
PreparedStatement ps = dbCon.prepareStatement(sql);
ps.setString(1,this.CatName);
ps.setInt(2,this.Child);
ps.setString(3,this.Picture);
ps.setInt(4,this.Display);
int result = ps.executeUpdate();

try that,
ghost
0
 
jarasaCommented:
Shouldn't you close the statement before closing the connection??

Class.forName(dbDriver);
dbCon = DriverManager.getConnection(dbURL);

String sql = "INSERT INTO Main (CatName,Child,Picture,Display) values (?,?,?,?)";
PreparedStatement ps = dbCon.prepareStatement(sql);
ps.setString(1,this.CatName);
ps.setInt(2,this.Child);
ps.setString(3,this.Picture);
ps.setInt(4,this.Display);
ResultSet rs = ps.executeQuery();
// Close the Prepared statement
ps.close();
// Close the Connection
dbCon.close();

Or put it on the finnally.

Javier
0
 
MsbathijaAuthor Commented:
hey guys

i did what nick asked me to change the query statement, after he had asked me to.
I need to though check for the absolute path thingy ..

One more thing, can it be  some server problem I am using Blazix web server ...
I doubt no server would be an issue.

Best Regards
0
 
copyPasteGhostCommented:
you can try tomcat?
that's the standard.
Ghost
0
 
MsbathijaAuthor Commented:
hey Ghost,

So would it be  a server problem for sur ethen ?

0
 
copyPasteGhostCommented:
well I don't know about it being the server for sure but it's possible another thing I noticed...your bean is not in a package.. beans should always be in a package.. and the .class file should be under your WEB-INF/classes/packageName/MyBean.class
that might be it...
You can try.
Ghost
0
 
MsbathijaAuthor Commented:
hey there ghost, i have now placed teh bean in the package and guess what still the same problem doesnt re-direct ????

hey u know what i removed the <jsp:forward> tag and I wrote <%=user.getCatName()%> and it prints is properly plus on the blazix web server window, it didnt report any errors ???

Like what surprises me then what can be the problem .. a simple <jsp:Forward> tag shouldnt be an issue ??

I really apprecaitea ll ur help, thanks man
0
 
copyPasteGhostCommented:
...wait a second... if you do this..
<jsp:useBean id="user" class="InsertCatBean" scope="session"/>
<jsp:setProperty name="user" property="*"/>

<%
try
{
user.doInsert();
%>
hi hi
<%
}


catch (Exception e)
{
out.println("THIS IS A CATCH");
System.out.println(e.getMessage());
e.printStackTrace();

}
}

%>

are there any errors that get printed?
Ghost
0
 
MsbathijaAuthor Commented:
doesnt print any  error at all
what can be the problem
0
 
copyPasteGhostCommented:
but the words..

"hi hi"
show up?
Ghost
0
 
MsbathijaAuthor Commented:
it did show
0
 
MsbathijaAuthor Commented:
it does print hi hi without any error
0
 
copyPasteGhostCommented:
are you sure the page name is right and is located at the same level as all the other pages.
Ghost
0
 
searlasCommented:
From this comment:
http://www.experts-exchange.com/Web/Web_Languages/JSP/Q_20958965.html#10859639

It looks like the page has been forwarding onto the adminhome.jsp page correctly, but that the adminhome.jsp is throwing an error.   I don't think you've shown us the code for your adminhome.jsp yet have you?

That's based on this fragment of stacktrace:
javax.servlet.ServletException: Exception thrown in JSP
        at desisoft.jsp.PageContext.handlePageException(PageContext.java:297)
        at desisoft_jsp_adminhome_jsp1082186836578._jspService(desisoft_jsp_admi
nhome_jsp1082186836578.java:290)
0
 
MsbathijaAuthor Commented:
thanks a lot man what u said was bang right... apparently, i didnt initialise a session varibale for the login...

Ghost, my very sincere thanks to you man, u really helped me ...

thanks a lot guys
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 14
  • 11
  • 3
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now