0

i am tring to avoid java code in jsp file. But i couldnt figure the solution. Can anyone help me?

JSP file :

<select>
<%
try
{
Class.forName("oracle.jdbc.driver.OracleDriver");
String url="jdbc:oracle:thin:@localhost:1521:xe";
String username="sys as sysdba";
String password="sys";
String query="select lecturerFullname from lecturer";
Connection con=DriverManager.getConnection(url,username,password);
Statement stmt=con.createStatement();
ResultSet rs=stmt.executeQuery(query);
while(rs.next())
{

%>

    <option><%=rs.getString("lecturerFullname") %></option>

        <%

}
%></select>
    </table>
    <%
    rs.close();
    stmt.close();
    con.close();
    }
catch(Exception e)
{
    e.printStackTrace();
    }




%></select>

This is what i tried... getAttribute() returns null for drop down list in jsp but i still couldnt find the solution. Do i have to use JSTL? can i avoid using JSTL hehe... I REALLY REALLY HOPE SOMEONE TO HELP ME. THANK YOU!!!!

Community
  • 1
  • 1
Mong2203
  • 71
  • 1
  • 12
  • Did you add the list or dataset to the servlet? – Babak Behzadi Feb 13 '16 at 06:06
  • i didnt because i really dont understand how List works – Mong2203 Feb 13 '16 at 06:09
  • DataSet is a cursor so you can iterate over that while its open!!! you better to iterate over dataset and fetch the rows and put them into a list of POJO class or entity class objects. For example http://stackoverflow.com/questions/1966836/resultset-to-list, if you need more info about list objects i hope this helps http://tutorials.jenkov.com/java-collections/list.html – Babak Behzadi Feb 13 '16 at 06:22
  • your welcome, i've added a sample as answer check it, i hope it helps – Babak Behzadi Feb 13 '16 at 06:40

3 Answers3

0

For example:

Lecturer POJO class:

class Lecturer {

    ...
    private String fullName;
    ...

   //getters and setters
}

DAO class:

class MyDAO {

   public List<Lecturer> getLecturers() {
     try{
        Class.forName("oracle.jdbc.driver.OracleDriver");
        String url="jdbc:oracle:thin:@localhost:1521:xe";
        String username="sys as sysdba";
        String password="sys";
        String query="select lecturerFullname from lecturer";
        Connection con=DriverManager.getConnection(url,username,password);
        Statement stmt=con.createStatement();
        ResultSet rs=stmt.executeQuery(query);

        List<Lecturer> list = new LinkedList<Lecturer>();

        while(rs.next()) {
            String fullname= rs.getString("lecturerFullname");
            Lecturer lec = new Lecturer();
            lec.setFullName(fullname);
            ...
            list.add(lec);
        }
     } catch(Exception e) {
     }
     return list;
   }    
}

In your servlet:

class MyServlet {

   MyDAO myDao = ...;

   doGet() {

      List<Lecturer> list = myDoa.getLecturers();
      request.setAttribute("list",list); 

   }
}

and finally in the jsp file, you need to get list and iterate over. ;)

EDITED

JSP file:

<select>
<%
   List<Lecturer> list = (List<Lecturer>) request.getAttribute("list");
   for(Lecturer lec : list) {
%>
   <option><%=lec.getFullName()%></option>
<%
   }
%>
</select>
Babak Behzadi
  • 1,236
  • 2
  • 16
  • 33
0

Create Servlet for the connection code and use JSTL to avoid Java code in the JSP file. Also use Request Dispatcher to set the values in an attribute in Servlet and then JSTL will allow you to get Attributes from the servlet with its own tag and that will solve your problem of avoiding Java code in JSPs.

Servlet Sample Code :

request.setAttribute("error_userID", "UserID is not Correct"); //setting error msg in an attribute request.getRequestDispatcher("/jsp/common/login.jsp").forward(request, response); //passing attribute to jsp

Sample JSP Code

< c:out value="${requestScope.error_userID}" >

tag named will let you set your dropdown value too in jsp and value comes through Servlet through request Object.

Enjoy Coding.. :) Thanks !!

0

Write as mentioned by Babak Behzadi

  1. Seperate DAO Class for DB connecivity
  2. Servlet
  3. JSP

In Your JSP file , Write JSTL Code... as

<select name="name" id="id-select">
    <c:forEach items="<%=request.getAttribute("list")%>" var="i"> 
                <option value="${i }">${i }</option>
    </c:forEach>
</select>

Additionally Add JSTL jars and import to your jsp file

try getting list using EL syntax items="${requestScope.list }"

Shantaram Tupe
  • 1,646
  • 3
  • 16
  • 42
  • Thank you sir :) I pasted your code for trial, but im getting error :Attribute value request.getAttribute("list") is quoted with " which must be escaped when used within the value....why is that? – Mong2203 Feb 13 '16 at 08:19
  • i know im doing this wrong ..... ` ` – Mong2203 Feb 13 '16 at 08:47
  • get list in attribute "items" try using EL synatx as given items="${requestScope.list }" – Shantaram Tupe Feb 13 '16 at 11:20
  • have a look at this http://stackoverflow.com/questions/27586676/to-print-list-values-from-servlet-to-jsp-using-cforeach-of-jstl – Shantaram Tupe Feb 13 '16 at 11:27