How to pass exception message to an HTML page from a JSP?

Asked

Viewed 488 times

1

I have a college exercise where I have to create a page. jsp that takes as parameter an "id" and runs a query to delete the record within the class products with this id and in case of any exception I send the user to page error.html passing as parameter the error text. The code I’ve made so far is this:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1" import="web.acesso.bd.*,web.acesso.bd.dao.*"%>
<html>  
<head><title>Insert title here</title></head>  
<body>   
<% 
int myId=request.getParameter("id")
try{
    bd.execComando ("DELETE FROM Filme id="+myId+");
} 
catch(Exception e)
{
   response.sendRedirect("erro.html");
}%>  
</body>  
</html>

So far I think it’s right, but I don’t know how to pass as parameter the exception message that is in the variable "and". Could someone help?

1 answer

4

Maybe something like this will solve:

<% 
   int myId=request.getParameter( "id" );

   try{
      bd.execComando ( "DELETE FROM Filme WHERE id=" + myId );
   } 
   catch( Exception e )
   {
      response.sendRedirect( "erro.html?excecao=" + URLEncoder.encode( e, "ISO-8859-1" ) );
   }
%>

Urlencoder will serve to convert spaces and special characters sa message into something that can be transmitted precisely by URL, you just need to set the conversion to what you need. Pus ISO-8859-1 for being the same one you used in the first line of the original code.

This is clear, assuming that the error page intended to process the parameter excecao to display to the end user.

  • 1

    Well, I didn’t know about this Urlencoder,?

  • I had a couple of syntax problems, I guess so solved.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.