File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

exception handling in JSP

 
akashkum singh
Greenhorn
Posts: 15
  • 0
  • Mark post as helpful
  • send pies
  • 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]
 
J. Kevin Robbins
Bartender
Pie
Posts: 1605
15
Chrome Firefox Browser jQuery Linux MySQL Database Netbeans IDE
  • 1
  • Mark post as helpful
  • send pies
  • 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.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64173
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
but how to solve this
 
Ulf Dittmer
Rancher
Pie
Posts: 42966
73
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How are you mapping the error page in your web.xml file?
 
J. Kevin Robbins
Bartender
Pie
Posts: 1605
15
Chrome Firefox Browser jQuery Linux MySQL Database Netbeans IDE
  • 0
  • Mark post as helpful
  • send pies
  • 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
Author and ninkuma
Marshal
Pie
Posts: 64173
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please also read this article to understand how to structure web apps properly.
 
Don't get me started about those stupid light bulbs.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic