• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

error message from servlet to jsp

 
lokesh jain
Ranch Hand
Posts: 58
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3908
9
Chrome Fedora Hibernate
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
lokesh jain
Ranch Hand
Posts: 58
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 757
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please Use Code Tags.
 
lokesh jain
Ranch Hand
Posts: 58
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please let me know how to use the request dispatcher,my code is:

 
Vijitha Kumara
Bartender
Posts: 3908
9
Chrome Fedora Hibernate
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 58
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry... I am done now...Thanks a million for help by all of you.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic