|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
help with sessions+servlets
im trying to get a servlet session goin for a college project,i know its hard to help someone on this but please try this is driving me mad,,,
im useless at java so i have just being piecing together other peoples code and trying to get it working, Iam trying to accept a username and password for the Oracle database and then put the resulting database connection into a session variable... here is my useless code,,,please help....the only error iam getting is to do with the PrintWriter,,, import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; // You need to import the java.sql package to use JDBC import java.sql.*; public class LoginServlet1 extends HttpServlet { private Connection con=null; public LoginServlet1(Connection con){ this.con=con; } public Connection getConnection(){ return con; } public void doGet(HttpServletRequest request,HttpServletResponse response) { PrintWriter writer = response.getWriter(); HttpSession session = request.getSession(true); String username = null; String password = null; String strHTML = ""; boolean success = false; if (((username==null) || (username.compareTo("")==0))) { strHTML = strHTML + "<title> Lab #11 Login Error </title>"; strHTML = strHTML + "</head>"; strHTML = strHTML + "<body bgcolor=white>"; strHTML = strHTML + "Massive Error --- you need to provide a username ...please try again ...<BR><BR>"; strHTML = strHTML + "Back to the login page ..<A HREF=\"../login.html\"> click here </A>"; strHTML = strHTML + "</body>"; strHTML = strHTML + "</html>"; success = false; } else if (((password==null) || (password.compareTo("")==0))) { strHTML = strHTML + "<title> Lab #11 Login Error </title>"; strHTML = strHTML + "</head>"; strHTML = strHTML + "<body bgcolor=white>"; strHTML = strHTML + "Massive Error --- you need to provide a password ... please try again ...<BR><BR>"; strHTML = strHTML + "Back to the login page ...<A HREF=\"../login.html\"> click here </A>"; strHTML = strHTML + "</body>"; strHTML = strHTML + "</html>"; success = false; } else { try { // Load Oracle driver DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver()); LoginServlet1 holder=(LoginServlet1) session.getValue("servletapp.connection"); if(holder==null){ try{ holder= new LoginServlet1(DriverManager.getConnection("jdbc racle:thin:@macha.wit.ie:1521:MACH"));session.putValue("servletapp.connection",holder); } catch(SQLException e){ getServletContext().log(e, "No"); } } success= true; } catch (SQLException sqlex) { strHTML = strHTML + "connection failed ....<BR>"; if (sqlex.getErrorCode()==1017) { strHTML = strHTML + "OUPS !!! wrong username or password lads ...please try again ...<BR><BR>"; } else { strHTML = strHTML + "SQL Error happened ....<BR>"; strHTML = strHTML + "Error Number = " +sqlex.getErrorCode() + "<BR>"; strHTML = strHTML + "Error Message = " + sqlex.getMessage() + "<BR>"; strHTML = strHTML + "ah common cheer up, it's not the end of the world...<BR>"; } strHTML = strHTML + "Back to the login page<A HREF=\"../login.html\"> click here </A>"; success = false; } } if (success) { try { getServletConfig().getServletContext().getRequestDispatcher("/WEB-INF/jsp/query.jsp").forward(request,response); } catch (Exception ex) { ex.printStackTrace (); } }} } |
|
#2
|
|||
|
|||
|
What error are you getting, you never say.
|
|
#3
|
|||
|
|||
|
error
i get the following error, compling using command prompt;
LoginServlet1.java:32: unreported exception java.io.IOException;must be caught or declared to be thrown PrintWriter writer=response.getWriter(); ^ Note:LoginServlet1.java uses or overrides a deprecated API. Note:Recompile with -deprecated for details 1 error |
|
#4
|
|||
|
|||
|
sessions
what modifications do i need to make to be able to use the sessions?
thanks <%@ page import="java.sql.*" %> <HTML> <CENTER> <B>Employees</B> <BR><BR> You have connected successfully!!! <FORM ACTION="query.jsp" METHOD="POST"> Enter your query: <INPUT TYPE="TEXT" NAME="query"><BR> <p><INPUT TYPE="SUBMIT" VALUE="Submit Query"> </html> <% Connection conn=null; try { String query= request.getParameter(query); Class.forName("oracle.jdbc.driver.OracleDriver"); conn = DriverManager.getConnection("jdbc racle:thin:@macha.wit.ie:1521:MACH","W99293251","YZQHUJ");Statement stmt = conn.createStatement(); ResultSet rs = stmt.executeQuery(query); //Print start of table and column headers out.println("<TABLE CELLSPACING=\"0\" CELLPADDING=\"3\" BORDER=\"1\">"); out.println("<TR><TH>ID</TH><TH>NAME</TH></TR>"); //Loop through results of query. while(rs.next()) { out.println("<TR>"); out.println("<TD>" + rs.getString("EMPNO") + "</TD>"); out.println("<TD>" + rs.getString("ENAME") + "</TD>"); out.println("<TD>" + rs.getString("JOB") + "</TD>"); out.println("<TD>" + rs.getString("SAL") + "</TD>"); out.println("</TR>"); } out.println("</TABLE>"); } catch(SQLException e) { out.println("SQLException: " + e.getMessage() + "<BR>"); while((e = e.getNextException()) != null) out.println(e.getMessage() + "<BR>"); } catch(ClassNotFoundException e) { out.println("ClassNotFoundException: " + e.getMessage() + "<BR>"); } finally { //Clean up resources, close the connection. if(conn != null) { try { conn.close(); } catch (Exception ignored) {} } } %> </CENTER> </BODY> </HTML> |
|
#5
|
|||
|
|||
|
You need to a catch block with the others for an IOException.
Code:
catch(SQLException e)
{
out.println("SQLException: " + e.getMessage() + "<BR>");
while((e = e.getNextException()) != null)
out.println(e.getMessage() + "<BR>");
}
catch(ClassNotFoundException e)
{
out.println("ClassNotFoundException: " + e.getMessage() + "<BR>");
}
/* add this */
catch(IOException e) {
e.printStackTrace();
}
|
|
#6
|
|||
|
|||
|
thanks
thanks for that but how do i use sessions with this page,,,
is the code the same for a jsp page as for a servlet,,,namely HttpSession session = request.getSession(true) and do u use the getAttribute() to get the database connection? |
|
#7
|
|||
|
|||
|
In jsp's you have an implicit variable called "session" available to you in the jsp. It is created by the compiler when making the jsp into a servlet.
Yes, you can just get the connection object out of the session with getAttribute if setAttribute was used to store it. Lastly, it is highly recommended that you *don't* store a database connection in a session. Databases generally have a limited number of connections available to be opened. Each session typically lasts 30 minutes. That menas it won't take very many hits and then users will be getting an error because the database cannot allocate any more connections. It is generally recommened that you either open a new connection and close the connection every request OR you should use connection pooling. Most app servers have some sort of connection pooling available to them. As for setting it up, it is beyond the scope of what I can tell you here. I would suggest searching for connection pooling and the name of the app server you are using on google to find it. |
![]() |
| Viewing: Dev Shed Forums > Programming Languages > Java Help > help with sessions+servlets |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|