Reputation: 148
I am new in JSP Servlet, the task i want to do is just using ResultSet data of servlet in JSP page... when i runs the code it is giving me errors like
org.apache.jasper.JasperException: PWC6033: Error in Javac compilation for JSP
PWC6199: Generated servlet error:
source value 1.5 is obsolete and will be removed in a future release
PWC6199: Generated servlet error:
target value 1.5 is obsolete and will be removed in a future release
PWC6199: Generated servlet error:
To suppress warnings about obsolete options, use -Xlint:-options.
PWC6197: An error occurred at line: 148 in the jsp file: /products.jsp
PWC6199: Generated servlet error:
cannot find symbol
symbol: class ResultSet
location: class org.apache.jsp.products_jsp
PWC6197: An error occurred at line: 148 in the jsp file: /products.jsp
PWC6199: Generated servlet error:
method processRequest in class jdbc.units cannot be applied to given types;
required: javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse
found: no arguments
reason: actual and formal argument lists differ in length
Here is my code..
units.java
package jdbc;
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 java.sql.*;
import java.util.*;
import java.util.logging.*;
import org.json.*;
import javax.servlet.http.HttpSession;
public class units extends HttpServlet {
protected ResultSet processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
ResultSet rs = null;
response.setContentType("text/html;charset=UTF-8");
try (PrintWriter out = response.getWriter()) {
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException ex) {
Logger.getLogger(units.class.getName()).log(Level.SEVERE, null, ex);
}
try {
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/gst_application", "root", "");
String sql = "SELECT * FROM `product_units` ORDER BY `product_unit_id` ASC";
PreparedStatement ps = null;
ps = con.prepareStatement(sql);
rs = ps.executeQuery();
} catch (SQLException ex) {
Logger.getLogger(units.class.getName()).log(Level.SEVERE, null, ex);
out.print(ex);
}
}
return rs;
}
}
product.jsp
<%@page import="jdbc.units"%>
<select class="form-control" name="product_unit" required="required">
<%
units obj = new units();
ResultSet rs = obj.processRequest();
while(rs.next())
{
out.print("<option>"+rs.getString("unit_val")+"</option>");
}
%>
</select>
Upvotes: 1
Views: 1564
Reputation: 172
You could put the ResultSet
object in a List and pass it to your page and in JSP extract this list. Here I am assuming Demo
is your class name. Uou can change code based on your class name. And do not forget to close the connections.
In Servlet
Demo d = null;
List <Demo> myList = new ArrayList<>();
while (rs.next()) {
myList.add(d);
}
rs.close();
ps.close();
con.close();
request.setAttribute("List", myList);
Forward your response to the JSP file like this:
RequestDispatcher requestDispatcher = request.getRequestDispatcher("/pageName.jsp");
requestDispatcher.forward(request, response);
In JSP
use this tag:
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
Access the list using forEach
<c:forEach items="${List}" var="theList">
<tr>
<td></td>
<td>${theList.attributeName}</td>
...
</tr>
</c:forEach>
Upvotes: 0
Reputation: 913
First a warning: You should not use ResultSet in your page directly. You should put the result in an intermediate object and then close ResultSet, Statement and if required the connection.
That being said, as far as I see you are missing import statement in your JSP page so JSP compiler cannot find the ResultSet class. Try importing jdbc packages in your JSP page.
Upvotes: 1