• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

exception handling in JSP

 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
index.html

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type"
content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>

<form action="process.jsp">
No1:
<input type="text" name="n1" />
<br />
<br />
No1:
<input type="text" name="n2" />
<br />
<br />
<input type="submit" value="divide" />
</form>
</body>
</html>

process.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type"
content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<%@ page errorPage="error.jsp"%>
<%
String num1 = request.getParameter("n1");
String num2 = request.getParameter("n2");

int a = Integer.parseInt(num1);
int b = Integer.parseInt(num2);
int c = a / b;
out.print("division of numbers is: " + c);
%>

</body>
</html>

error.jsp


<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type"
content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>

<%@ page isErrorPage="true"%>

<h3>
Sorry an exception occured!
</h3>

Exception is:
<%=exception%>

</body>
</html>


=======================================================
NOTE

when i insert 4 and 2 in both textbox it gives answer
but
when i insert 4 and 0 or 4 ans a respectively in txtbox ...it does not show error message infact it gives following error
error.JPG
[Thumbnail for error.JPG]
 
Bartender
Posts: 1810
28
jQuery Netbeans IDE Eclipse IDE Firefox Browser MySQL Database Chrome Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That's why there should never be code in a jsp, hence, no need for exception handling. Code belongs in servlets and POJO's, not jsp pages.
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

J. Kevin Robbins wrote:That's why there should never be code in a jsp, hence, no need for exception handling. Code belongs in servlets and POJO's, not jsp pages.



Quoted for truth. Using Java scriptlets in a JSP in 2014 is at this point simply irresponsible.
 
akashkum singh
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
but how to solve this
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How are you mapping the error page in your web.xml file?
 
J. Kevin Robbins
Bartender
Posts: 1810
28
jQuery Netbeans IDE Eclipse IDE Firefox Browser MySQL Database Chrome Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Start here. Look at some of the recommended reading. Then briefly I would advise this way.

You need a servlet that will receive the request from your index.html page. That's not the best design, but let's start off slow here until you get a little more comfortable. That servlet will take your integers "a" and "b" that you've sent as request parameters. You can then perform the divide in the servlet code (again, not the best design, this should be in a POJO, but we're staying simple) and put the result in a request attribute and then forward the request to your jsp page which will display the result using JSTL.

The important thing to note here is that all the logic processing is in the servlet. Again, I would put it in a POJO and make the servlet strictly a controller, but we're taking baby steps first before you start learning to run.

Look for examples of things like MVC, using a RequestDispatcher, and JSTL and give it a shot. Post your code and tell us what result you got and we'll guide you along the path to success.
 
Bear Bibeault
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please also read this article to understand how to structure web apps properly.
reply
    Bookmark Topic Watch Topic
  • New Topic