aspose file tools*
The moose likes Servlets and the fly likes error message from servlet to jsp Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "error message from servlet to jsp" Watch "error message from servlet to jsp" New topic
Author

error message from servlet to jsp

lokesh jain
Ranch Hand

Joined: Feb 21, 2009
Posts: 58
Hi, I am a newbie in servlets. I have made a jsp and a servlet. My design.JSP takes the inputs to connect to DB and then control goes to servlet and it is validated. If it is correct it goes to next page otherwise it will remain at the same jsp page. however I want to display the SQL Error message on the design.jsp at a particular place...can anyone help with the code?

Thanks in advance
meehul Chopra
Greenhorn

Joined: Feb 23, 2009
Posts: 10
lokesh jain wrote:Hi, I am a newbie in servlets. I have made a jsp and a servlet. My design.JSP takes the inputs to connect to DB and then control goes to servlet and it is validated. If it is correct it goes to next page otherwise it will remain at the same jsp page. however I want to display the SQL Error message on the design.jsp at a particular place...can anyone help with the code?

Thanks in advance

Hi lokesh,
Whenever in your servlet the user is not a valid user,,at that time
use
request.setAttribute("error","Invlaid user") //suppose

after this you will dispatch your request to design.jsp using RequestDispatcher
in which you will call
String errorMsg=(String)request.getAttribute("error") and print the errorMsg.

I would suggest that you read through, setting and getting Attributes in different scopes.
Vijitha Kumara
Bartender

Joined: Mar 24, 2008
Posts: 3825

lokesh jain wrote:however I want to display the SQL Error message on the design.jsp at a particular place...can anyone help with the code?


That's not a good way. You shouldn't show SQL Errors to the user, instead have your custom (meaningful) message to the user.


SCJP 5 | SCWCD 5
[How to ask questions] [Twitter]
lokesh jain
Ranch Hand

Joined: Feb 21, 2009
Posts: 58
hi meehul.
this is my code...please let me know how to apply dispatcher:

protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// processRequest(request, response);
Connection con =null;
String Destination="";



System.out.println("Entering the servlet ");
String ServerName= request.getParameter("Server");
System.out.println("value of param 1 " + ServerName);
String DBName= request.getParameter("DBName");
System.out.println("value of param 2 " + DBName);
String UserName= request.getParameter("User");
System.out.println("value of param 3 " + UserName);
String Password= request.getParameter("Pass");
System.out.println("value of param 4 " + Password);
try{

if(null!=request.getParameter("next")) {

Class.forName("oracle.jdbc.driver.OracleDriver");
String DBUrl = "jdbcracle:thin:@"+ServerName+":1521:"+DBName ;
con = DriverManager.getConnection(DBUrl,UserName,Password);
Statement stmt = con.createStatement ();


if(null!=con)
{
System.out.println("Connection Successful");
request.setAttribute("userMessage", "Welcome User");
con.close();
Destination="QueryBuilder.jsp";
response.sendRedirect(response.encodeRedirectURL(Destination));
}
System.out.println("Leaving the Servlet");
}
else{
request.setAttribute("ErrorMessage", "Sorry,DB details are wrong");
String errorMsg=(String)request.getAttribute("ErrorMessage");
RequestDispatcher dispatcher = request.getRequestDispatcher(errorMsg);

}

}catch(Exception e)
{
System.out.println("Exception occured "+e);
e.printStackTrace();
// request.setAttribute("Sorry, the details are wrong", e);
// Destination="design.jsp";
// response.sendRedirect(response.encodeRedirectURL(Destination));
// request.getAttribute("Sorry, the details are wrong");
// //System.out.println("Exception occured "+e);
// RequestDispatcher dispatcher = request.getRequestDispatcher("design.jsp");
// dispatcher.forward( request, response );

}
finally
{
if(null!=con)
{
try {
con.close();
} catch (SQLException sqe) {
System.out.print("Sorry, details are wrong" +sqe);
}
}
}
}
Treimin Clark
Ranch Hand

Joined: Nov 12, 2008
Posts: 757
Please Use Code Tags.
lokesh jain
Ranch Hand

Joined: Feb 21, 2009
Posts: 58
Please let me know how to use the request dispatcher,my code is:

Vijitha Kumara
Bartender

Joined: Mar 24, 2008
Posts: 3825

lokesh jain wrote:Please let me know how to use the request dispatcher,my code is:



Here you are using the RequestDispatcher. What's the problem you are having?
lokesh jain
Ranch Hand

Joined: Feb 21, 2009
Posts: 58
Sorry... I am done now...Thanks a million for help by all of you.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: error message from servlet to jsp