• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to handle exception in jsp

 
Arun Maalik
Ranch Hand
Posts: 216
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear sir as we know that the function which produce checked exception you most be the catch in catch exception but upto in core java i know.
well as far as jsp is concerned like see following coding
<%@ page language="java"%>
<%@ page import="java.sql.*"%>

<html>
<body>

<%
try{
String str1="select ename from emp where empno=?" ;
String str2=request.getParameter("text");
int n=Integer.parseInt(str2.trim());
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con=DriverManager.getConnection ("jdbcdbc:MyDataSource","scott","tiger");
PreparedStatement ps=con.prepareStatement(str1);
ps.setInt(1,n);
ResultSet rs=ps.executeQuery();
rs.next();
String str3=rs.getString("ename");
out.println("Employee name is ");
out.println(str3);
}
catch(Exception e){
e.printStackTrace();
}
%>
</body>
</html>

since above statment is not working even no error

will it work , or if i will ad a another page which is concerned with exception like with function exception.getClass() and if i will remove catch statment from this page then what happen.and if work then why.
okay finally i would like to know that what happen if i will not catch checked exception in same page and if work then why it is breking rules of core java.

with regard

Arun kumar maalik
 
Ulf Dittmer
Rancher
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
above statment is not working

What does this mean? No output? Too much output? Incorrect output? Is the correct statement sent to the DB? TellTheDetails
 
nizams uddin
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,

not able to understand your question.

but think yu are talking abuot exception being thrown in jsp.
if this is thte qs, then pls check the code.
i think the str2 value might be null and you are doing a trim on str2.
pls check the value of str2 and then proceed.
 
Arun Maalik
Ranch Hand
Posts: 216
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sorry sir Actualy i would like to know that in jsp you can handle your error or exception by including another error page okay.
now see
suppose i have make a jsp in whic i am using a function which is throwing a checked exception now as by rules you most catch that checked exception like see
try{
Thread.sleep(100)
}
catch(InterruptedException e){}//You most have to do this

but on the other hand in jsp you can implement same exception by including other error page without implementing the catch clause in same page
like
<%@page language="java"%>
<%@ page errorPage="hhh.jsp"%>
<%
Thread.sleep(100)
finall{
//some code herer
}
%>//no catch here

and the implemention of hhh.jsp page is as

<%@ page isErrorPage="true"%>
<%=error.getClass()%>
<%=error.getMessage()%>

and it will work why sir although we know that we most have to catch the checked exception but here we are not catching.

I would like to know this

with regard

Arun kumar maalik
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Arun,
Javaranch tip:

If you are going to post more than a line or two of your code, wrap that
code in a set of UBB Code tags.
Doing so will help to preserve your code's indenting, making it easier to read.
If it is easier to read, more people will actaully read it and you will
stand a better chance of getting help with your question.
See UseCodeTags for more
help with UBB code tags.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64833
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
nizams uddin,

We appreciate that you are trying to help and answer questions here on the Ranch, but your posts will be more effective if you use real words rather than SMS contractions like yu, pls, and qs.

Such abbreviations are not accepatable. Please read this for more information.

thanks,
bear
JavaRanch Sheriff
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic