Skip to Main Content

Java EE (Java Enterprise Edition) General Discussion

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

How to reture error message to browns when use RequestDispatcher

843840Feb 19 2003 — edited Feb 21 2003
Hi:
I use html+ servlet +jsp to insert data(like SSN address...) into member registration table and forward data back to browns. Since,I set SSN as primary key so when the input ssn is uniqe everything work fine, but my program can't forward error message to jsp file when ssn is already in table. what do I do wrong in my code ?

/////////////////////////////////////////

public void doPost(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {

res.setContentType("text/html");
PrintWriter out = res.getWriter();

try {


//Get the incoming data parameters from the client
String NewSSN =req.getParameter("SSN");
String Fname =req.getParameter("Fname");

String ErrMsg = "";


conn = DriverManager.getConnection( "jdbc:microsoft:sqlserver://localhost:1433; DatabaseName=airplanes","****","*****" );
DatabaseMetaData md = conn.getMetaData();
stmt = conn.createStatement(
ResultSet.TYPE_SCROLL_SENSITIVE,
ResultSet.CONCUR_READ_ONLY);

rs = stmt.executeQuery("select SSN FROM OurMembers ");

// user information will be insert into our database
//if we can't find your ssn in our database

while(rs.next()) {
if (NewSSN = SSN){
ErrMsg ="you registrate before or you don't enter the right SSN";
req.setAttribute("ErrMsg",ErrMsg);}

else{
stmt.executeUpdate("INSERT INTO OurMembers ("
+ "SSN, Fname)"
+ "VALUES('"
+ NewSSN + "','"
+ Fname + "'"
+ ")");


//get current session, create if it doesn't exits
HttpSession userSession = req.getSession(true);
userSession.setAttribute("SSN", NewSSN);
userSession.setAttribute("Fname", Fname);

}

}//end of while


RequestDispatcher disp;
disp = getServletContext().getRequestDispatcher("/Welcome.jsp");

disp.forward(req, res);
}//end try


catch(SQLException e) {

}

finally {
try {
if(rs != null)
{
rs.close();
rs = null;
}
if(stmt != null)
{
stmt.close();
stmt = null;
}
if(conn != null)
{
conn.close();
conn = null;
}
} //end try
catch (SQLException e) {}

}//end finally
}

}
Thank you
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Mar 21 2003
Added on Feb 19 2003
13 comments
330 views