Results 1 to 3 of 3
Like Tree1Likes
  • 1 Post By Tolls

Thread: How to run boilerplate code in JSP from a class

  1. #1
    megabull is offline Member
    Join Date
    Aug 2012
    Posts
    22
    Rep Power
    0

    Default How to run boilerplate code in JSP from a class

    Hey everyone
    I was recently working on a JSP page when i noticed that there's a lot of boilerplate code in the set of jsp pages i was making, primarily the jdbc connectivity code. Now, i decided to make a class, import it, and make a function inside the class that would run the boilerplate code. now, the thing is that, am not able to run the resultset onwards code in jsp.


    The error i get on executing the JSP is "s cannot be resolved" and "rs cannot be resolved to a variable".

    JSP file

    Java Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <%@page language="java"%>
    <%@page import="java.sql.*"%>
    <%@page import="java.lang.*"%>
    <%@page import="java.io.*"%>
    <%@ page import="loginn.logss" %>
    <html>
    <head>
    </head>
    <body>
    <form method="post" action="update.jsp">
    <table border="1">
    <tr><th>Id</th><th>Name</th><th>Address</th><th>Contact No</th><th>Email</th></tr>
    <%
    logss lm;
    lm.logs();
    rs=s.executeQuery( "select * from userdata where id='"+ (session.getAttribute("username")) +"';")
    while(rs.next()){
    	%>
    	<tr>
    	<td><input type="text" name="id" value="<%=rs.getString("id")%>"></td>
    	<td><input type="text" name="name" value="<%=rs.getString("name")%>"></td>
    	<td><input type="text" name="address" value="<%=rs.getString("address")%>"></td>
    	<td><input type="text" name="contact" value="<%=rs.getInt("contact")%>"></td>
    	<td><input type="text" name="email" value="<%=rs.getString("email")%>"></td>
    	</tr>
    	<tr>
    	<td><input type="submit" name="Submit" value="Update" style="background-color:#49743D;font-weight:bold;color:#ffffff;"></td>
    	</tr>
    	<% request.setAttribute("al",session.getAttribute("username"));   %>
    </table>
    <%
    }
    %>
    </form>
    <button onclick="window.location = 'logged.jsp'"> Goto dashboard</button>
    </body>
    </html>

    The Class file
    Java Code:
    Class filepackage loginn;
    
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.ResultSet;
    import java.sql.Statement;
    import java.io.*;
    
    public class logss {
    	public void logs()
    	{
    	
    	Class.forName("com.mysql.jdbc.Driver").newInstance();
    	Connection connect = DriverManager.getConnection ("jdbc:mysql://localhost:3306/userdb","root","magic");
    	Statement s = connect.createStatement();
    	ResultSet rs;
    		}
    
    }

    ps: Using classes in JSP isn't something i'd like to do, i've been asked to do so.

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,830
    Rep Power
    19

    Default Re: How to run boilerplate code in JSP from a class

    Whoever asked you to do that is a twit.
    If you're learning then you should be learning Servlets first, then utilising JSPs to display the results.
    Writing code in JSPs like that is just plain wrong.

    But that little rant aside,

    Java Code:
    logss lm;
    lm.logs();
    rs=s.executeQuery( "select * from userdata where id='"+ (session.getAttribute("username")) +"';")
    First you never initialise lm, so you'll get a compile error on the lm.logs() line.
    Second 's' is a local variable in lm, so you can't access it at all.

    Java Code:
    public class logss {
    	public void logs()
    	{
    	
    	Class.forName("com.mysql.jdbc.Driver").newInstance();
    	Connection connect = DriverManager.getConnection ("jdbc:mysql://localhost:3306/userdb","root","magic");
    	Statement s = connect.createStatement();
    	ResultSet rs;
    		}
    
    }
    the statement never gets out of this code (and neither does the result set).
    And you're going to get leaks. Your connection is never closed, and neither is the statement, or the result set. It'll be a toss-up whether you run out of cursors or connections first.

    Your best bet is to change your logss class, adding a getLogs method that does the whole thing, returning a List<LogView>, where a LogView is an object representing the data you want to display. That way you can open and close the connection/statement/resultset all in the single method and remove the chances of a leak (if you do it correctly). Then the JSP can loop over this List and display the data.
    wsaryada likes this.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    wsaryada is offline Senior Member
    Join Date
    Jun 2007
    Location
    Bali, Indonesia
    Posts
    757
    Rep Power
    8

    Default Re: How to run boilerplate code in JSP from a class

    Quote Originally Posted by Tolls View Post
    Whoever asked you to do that is a twit.
    If you're learning then you should be learning Servlets first, then utilising JSPs to display the results.
    Writing code in JSPs like that is just plain wrong.
    Couldn't agree more with this statement. In the servlet world, JSP must be used to focus on presentation. And all the business logic should be place inside servlets.

Similar Threads

  1. Find the Error in Class Code
    By Humphrey Bogart in forum New To Java
    Replies: 3
    Last Post: 03-30-2011, 04:43 PM
  2. how to separate this code in another class
    By Jhovarie in forum AWT / Swing
    Replies: 5
    Last Post: 03-01-2011, 05:08 PM
  3. Deleted Class template code. Now I cant make class.
    By AcousticBruce in forum IntelliJ IDEA
    Replies: 0
    Last Post: 01-11-2011, 10:52 PM
  4. Hibernate boilerplate
    By karq in forum Web Frameworks
    Replies: 1
    Last Post: 10-18-2010, 10:31 AM
  5. how to see the code of class files
    By mary in forum Advanced Java
    Replies: 1
    Last Post: 08-02-2007, 01:52 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •