aspose file tools*
The moose likes Servlets and the fly likes exception handling in JSP Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "exception handling in JSP" Watch "exception handling in JSP" New topic
Author

exception handling in JSP

akashkum singh
Greenhorn

Joined: Mar 10, 2014
Posts: 15
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


[Thumbnail for error.JPG]

J. Kevin Robbins
Bartender

Joined: Dec 16, 2010
Posts: 1043
    
  13

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.


"The good news about computers is that they do what you tell them to do. The bad news is that they do what you tell them to do." -- Ted Nelson
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61657
    
  67

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.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
akashkum singh
Greenhorn

Joined: Mar 10, 2014
Posts: 15
but how to solve this
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42632
    
  65
How are you mapping the error page in your web.xml file?


Ping & DNS - my free Android networking tools app
J. Kevin Robbins
Bartender

Joined: Dec 16, 2010
Posts: 1043
    
  13

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

Joined: Jan 10, 2002
Posts: 61657
    
  67

Please also read this article to understand how to structure web apps properly.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: exception handling in JSP