Solved

url not working

Posted on 2007-11-23
15
287 Views
Last Modified: 2010-03-30
i have a link in jsp when i click on it , i get the following in the browser
http://localhost:7004/myapps/MyServlet?id=25&filename=abcd%20def%20ijk-00-02-6789.xls

and i get 404 error !.

but the servlet is there .

when i call using http://localhost:7004/myapps/MyServlet?id=25 i get Nullpointer exception.

how to modify the url in the jsp so that it can call the servlet.

basically i am trying to send  one id and file name through the link to the servlet

0
Comment
Question by:cofactor
  • 8
  • 5
  • 2
15 Comments
 
LVL 4

Expert Comment

by:jcoombes
ID: 20337799
Do you have a copy of your web.xml file - can you post it?
0
 
LVL 16

Expert Comment

by:gnoon
ID: 20337800
I think the servlet is success invoked, but it forwards to another page with wrong path reference.
0
 
LVL 4

Expert Comment

by:jcoombes
ID: 20337834
Hmmm...yeah you're right.  the NPE is probably because it's missing the filename parameter....
0
 

Author Comment

by:cofactor
ID: 20337907
yea...i have web.xml

<servlet-mapping>
    <servlet-name>MyServlet</servlet-name>
    <url-pattern>/MyServlet</url-pattern>
  </servlet-mapping>

yea..i am getting NPE .

if the value of param name has blanks , can it create problem ?
is it because it has an .xls in value.

not sure how to go about it
0
 

Author Comment

by:cofactor
ID: 20337913
>I think the servlet is success invoked, but it forwards to another page with wrong path reference

hello Master, can you please clarify what you  meant here ?

can i cross check ?
0
 

Author Comment

by:cofactor
ID: 20337966
Do you want to see my servlet ?

here it is

this is the content of doPost method

                ServletOutputStream out = response.getOutputStream();
            response.setContentType("application/x-download");
            String rowid=(String)request.getParameter("id");
            String fileName=(String)request.getParameter("filename");
            response.setHeader("Content-Disposition", "attachment; filename=" + fileName);
            DownloadUtil dutil =new DownloadUtil();
            byte[] excelbyte=dutil.selectBlob(rowid);
            out.write(excelbyte);
0
 
LVL 16

Expert Comment

by:gnoon
ID: 20337986
404 error is probably know as Page Not Found Error, but the servlet is found. So, it should be another page.

The possible case is you have code like this at the end of the servlet

response.sendRedirect( anotherPageURL );

or

request.getRequestDispatcher( anotherPageURL ).forward(request, response);

// anotherPageURL  can be either relative or full URL. However, it may be not there.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:cofactor
ID: 20338005
i dont have any page forward at the end of the servlet in fact.

i dont want that also.

i want to download excel .

i have got the excel  byte[]  array from the DB blob and put that byte[] into ServletOutputStream so that user might get a download prompt .

and so i did not put any page forwarding.

please look at the above code i posted.

0
 
LVL 16

Expert Comment

by:gnoon
ID: 20338033
>this is the content of doPost method
But you have send parameters via GET method. Is it really be called? Should it be doGet?

>yea..i am getting NPE .
As code above, I see only one line that may throw the error.
byte[] excelbyte=dutil.selectBlob(rowid); // inside selectBlob somewhere
0
 

Author Comment

by:cofactor
ID: 20338048
>But you have send parameters via GET method. Is it really be called? Should it be doGet?

doGet  is calling doPost() ...so no worry .

public void doGet(HttpServletRequest request, HttpServletResponse response)
                  throws ServletException, IOException {

            doPost(request,response);
      }


>byte[] excelbyte=dutil.selectBlob(rowid); // inside selectBlob somewhere

very much unlikely ....as i have printed all the stacktraces ....no exception is printed.
0
 
LVL 16

Expert Comment

by:gnoon
ID: 20338095
To cross check, simple write something to the browser and comment code above.
response.setContentType("text/html;charset=UTF-8");

PrintWriter out = response.getWriter();

out.print("<h1>You're trying to export excel file.</h1>");

out.close();

Open in new window

0
 

Author Comment

by:cofactor
ID: 20338180
i made a mistake in the servlet code and so it was not geeting called.

basically , i made a mistake in the servletname and mapping and servlet class....there was a capslock problem......anyway, things has been resolved now.

when i call servlet , i get a debug prompt in the servlet but i am surprised to see
byte[] excelbyte=dutil.selectBlob(rowid);  // excelbyte =null !!!!!!!!


i dont know why its coming as null .

though i am getting Download prompt but nothing is saving and also not getting .xls file.
0
 

Author Comment

by:cofactor
ID: 20338187
here i am sending the selectBlob method
public byte[] selectBlob(String rowid) {
 

		ResultSet rs = null;

		Statement stmt = null;

		Connection connection = null;

		byte[] returndata = null;
 

		try {
 

			connection = (new DBUtil()).getConnection();

			stmt = connection.createStatement();
 

			String sql = "SELECT rowid, blob_field FROM blob_repository WHERE rowid = "

					+ rowid;
 

			rs = stmt.executeQuery(sql);
 

			if (rs.next()) {
 

				try {

					ByteArrayOutputStream baos = new ByteArrayOutputStream(1024);

					BufferedInputStream bis = new BufferedInputStream(rs

							.getBinaryStream("blob_field"));

					byte bindata[] = new byte[1024];

					int bytesread = 0;
 

					if (!rs.wasNull()) {
 

						if ((bytesread = bis.read(bindata, 0, bindata.length)) != -1) {

							baos.write(bindata, 0, bytesread);
 

						} else {

							returndata = baos.toByteArray();
 

						}

					}
 

					bis.close();
 

				} catch (IOException ioe) {

					System.err.println("Problem retrieving binary data: " + ioe);

				}

			}
 

		} catch (SQLException se) {

			System.err.println("Couldn't retrieve binary data: " + se);

		} finally {

			try {
 

				if (rs != null)

					rs.close();

				if (stmt != null)

					stmt.close();

				if (connection != null)

					connection.close();
 

				connection.close();

			} catch (SQLException e) {

				// TODO Auto-generated catch block

				e.printStackTrace();

			}

		}
 

		return returndata;

	}

Open in new window

0
 

Author Comment

by:cofactor
ID: 20338199
i called that method in the servlet

byte[] excelbyte=dutil.selectBlob(rowid); //excelbyte==null  :(

and also not getting .xls  file extension


0
 
LVL 16

Accepted Solution

by:
gnoon earned 125 total points
ID: 20338502
I cant understand your code coz never used rs.wasNull before. Please try this code
public byte[] selectBlob(String rowid)

{

    byte[] returndata = null;

    Connection connection = null;

    try

    {

        // Prepare a Statement:

        connection = new DBUtil().getConnection();

        Statement stmt = connection.createStatement();
 

        // Execute

        ResultSet rs = stmt.executeQuery(

            "SELECT rowid, blob_field FROM blob_repository WHERE rowid = "+rowid);
 

        if(rs.next())

        {

            try

            {

                // Get as a BLOB

                Blob aBlob = rs.getBlob(2);

                returndata = aBlob.getBytes(1, (int) aBlob.length());

            }

            catch(Exception ex)

            {

                // The driver could not handle this as a BLOB...

                // Fallback to default (and slower) byte[] handling

                returndata = rs.getBytes(2);

            }

        }
 

        // Close resources

        rs.close();

        stmt.close();
 

    }

    catch(Exception ex)

    {

        System.err.println("Couldn't retrieve binary data: " + ex);

    }

    finally

    {

        try{connection.close();}catch(Exception e){}

    }
 

    return returndata;

}

Open in new window

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
word0 challenge 3 78
Modeling a class in java 5 46
eclipse argument 14 53
Java Options in Zimbra Server 2 44
For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …

912 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

21 Experts available now in Live!

Get 1:1 Help Now