Win a copy of Micro Frontends in Action this week in the Server-Side JavaScript and NodeJS forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

error message from servlet to jsp

 
Ranch Hand
Posts: 58
  • 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
 
Greenhorn
Posts: 10
  • 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.
 
Bartender
Posts: 4107
72
Hibernate Fedora Chrome
  • 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
  • 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 = "jdbc:oracle: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);
}
}
}
}
 
Ranch Hand
Posts: 757
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please Use Code Tags.
 
lokesh jain
Ranch Hand
Posts: 58
  • 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: 4107
72
Hibernate Fedora Chrome
  • 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
  • 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.
 
Oh. Hi guys! Look at this tiny ad:
the value of filler advertising in 2020
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic