Solved

Response.sendRedirect doesnt work after inserting record successfully ?

Posted on 2004-04-19
33
2,052 Views
Last Modified: 2012-05-04
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
Comment
Question by:Msbathija
  • 14
  • 11
  • 3
  • +2
33 Comments
 

Author Comment

by:Msbathija
ID: 10858928
Sorry guys, the extra brace after the response.sendRedirect() method was for my else block. Please ignore that, thanks.
0
 
LVL 9

Expert Comment

by:Nick_72
ID: 10858966
>> 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
 

Author Comment

by:Msbathija
ID: 10859029
hey nick,

that one was a typing glicth, but yes it does insert a record. the probelem lies with the redirect
0
 
LVL 13

Expert Comment

by:copyPasteGhost
ID: 10859045
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
 

Author Comment

by:Msbathija
ID: 10859096
hey there..

i did try the same thing before and it didnt work... i tried it one more time and still it doesnt
0
 
LVL 7

Expert Comment

by:searlas
ID: 10859105
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
 
LVL 13

Assisted Solution

by:copyPasteGhost
copyPasteGhost earned 65 total points
ID: 10859107
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
 

Author Comment

by:Msbathija
ID: 10859288
nopes it doesnt work still.. after inserting it still remains on admincat.jsp?add=1
0
 
LVL 13

Expert Comment

by:copyPasteGhost
ID: 10859333
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
 

Author Comment

by:Msbathija
ID: 10859379
hey there, it inserts correctly and also displays the "this is catch" statement on the browser. what might be causing the problem ?
0
 
LVL 13

Expert Comment

by:copyPasteGhost
ID: 10859548
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
 
LVL 9

Expert Comment

by:Nick_72
ID: 10859572
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
 

Author Comment

by:Msbathija
ID: 10859639
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
 
LVL 13

Expert Comment

by:copyPasteGhost
ID: 10859654
can you post your entire jsp page please?
Ghost
0
 

Author Comment

by:Msbathija
ID: 10859710
<%@ 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
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
LVL 9

Expert Comment

by:Nick_72
ID: 10859793
not sure if it'll help, but try make the path absolute (beginning with a /).

<jsp:forward page="/adminhome.jsp"/>
0
 
LVL 13

Expert Comment

by:copyPasteGhost
ID: 10859826
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
 
LVL 6

Expert Comment

by:jarasa
ID: 10860629
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
 

Author Comment

by:Msbathija
ID: 10861621
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
 
LVL 13

Expert Comment

by:copyPasteGhost
ID: 10861665
you can try tomcat?
that's the standard.
Ghost
0
 

Author Comment

by:Msbathija
ID: 10866075
hey Ghost,

So would it be  a server problem for sur ethen ?

0
 
LVL 13

Expert Comment

by:copyPasteGhost
ID: 10867567
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
 

Author Comment

by:Msbathija
ID: 10868239
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
 
LVL 13

Expert Comment

by:copyPasteGhost
ID: 10868626
...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
 

Author Comment

by:Msbathija
ID: 10868680
doesnt print any  error at all
what can be the problem
0
 
LVL 13

Expert Comment

by:copyPasteGhost
ID: 10868694
but the words..

"hi hi"
show up?
Ghost
0
 

Author Comment

by:Msbathija
ID: 10868725
it did show
0
 

Author Comment

by:Msbathija
ID: 10868801
it does print hi hi without any error
0
 
LVL 13

Expert Comment

by:copyPasteGhost
ID: 10869113
are you sure the page name is right and is located at the same level as all the other pages.
Ghost
0
 
LVL 7

Accepted Solution

by:
searlas earned 65 total points
ID: 10869220
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
 

Author Comment

by:Msbathija
ID: 10876719
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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Short answer to this question: there is no effective WiFi manager in iOS devices as seen in Windows WiFi or Macbook OSx WiFi management, but this article will try and provide some amicable solutions to better suite your needs.
In this article, I will show you HOW TO: Install VMware Tools for Windows on a VMware Windows virtual machine on a VMware vSphere Hypervisor 6.5 (ESXi 6.5) Host Server, using the VMware Host Client. The virtual machine has Windows Server 2016 instal…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now