1

I am new to Java.
I created a project below with Java Dynamic Web - servlet and it works.

@WebServlet("/the_data/userdata/userData")
public class userData extends HttpServlet {
    private static final long serialVersionUID = 1L;


    protected void doGet(HttpServletRequest request, HttpServletResponse response) 
            throws ServletException, IOException {

        response.setContentType("text/html");

        //query
        String query = "select * from country";

        try {
            //Note -> ConLib is my connection class
            ResultSet rst = ConLib.connect().createStatement().executeQuery(query);

            response.getWriter().print("<table>");
            response.getWriter().print("<tr>");
            while(rst.next()) {
                response.getWriter().print("<tr>");
                response.getWriter().print("    <td>src/"+rst.getString("id")+"</td>");
                response.getWriter().print("    <td>"+rst.getString("countryName")+"</td>");
                response.getWriter().print("    <td>"+rst.getString("countryName")+"</td>");
                response.getWriter().print("</tr>");
            }
            response.getWriter().print("</tr>");
            response.getWriter().print("</table>");

        } 
        catch (SQLException e) {
            e.printStackTrace();
        }

    }
}



Writing an html page like above is really hard.
I decide to write new project (Spring MVC Project). I am converting above code into Spring MVC Framework. I have no idea, the code doesn't work. I just want to write the array in country_view.jsp file.

@RequestMapping(value = "/country", method = RequestMethod.GET)
public String pasar(Model model) throws SQLException {

    String theSQL = "select * from country";

    //Note -> ConLib is my connection class
    ResultSet rst = ConLib.connect().createStatement().executeQuery(theSQL);

    model.addAttribute(rst);

    return "country_view";
}

This is country_view.jsp

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ page session="false" %>
<html>
<head>
    <title>Country</title>
</head>
<body>
    <h1>Country</h1>

    <% while(rst.next()) { %>
        <%= rst.getString("countryName") %>
    <% } %>
</body>
</html>

This is error page:

type Exception report

message Unable to compile class for JSP:

description The server encountered an internal error that prevented it from fulfilling this request.

exception

org.apache.jasper.JasperException: Unable to compile class for JSP: 

An error occurred at line: 10 in the jsp file: /WEB-INF/views/country_view.jsp
rst cannot be resolved
7: <body>
8: <h1>Country</h1>
9:  
10:  <% while(rst.next()) { %>
11:     <%= rst.getString("countryName") %>
12:  <% } %>
13: </body>


An error occurred at line: 11 in the jsp file: /WEB-INF/views/country_view.jsp
rst cannot be resolved
8: <h1>Country</h1>
9:  
10:  <% while(rst.next()) { %>
11:     <%= rst.getString("countryName") %>
12:  <% } %>
13: </body>
14: </html>


Stacktrace:
    org.apache.jasper.compiler.DefaultErrorHandler.javacError(DefaultErrorHandler.java:103)
    org.apache.jasper.compiler.ErrorDispatcher.javacError(ErrorDispatcher.java:366)
    org.apache.jasper.compiler.JDTCompiler.generateClass(JDTCompiler.java:468)
    org.apache.jasper.compiler.Compiler.compile(Compiler.java:378)
    org.apache.jasper.compiler.Compiler.compile(Compiler.java:353)
    org.apache.jasper.compiler.Compiler.compile(Compiler.java:340)
    org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:646)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:357)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
    org.springframework.web.servlet.view.InternalResourceView.renderMergedOutputModel(InternalResourceView.java:238)
    org.springframework.web.servlet.view.AbstractView.render(AbstractView.java:262)
    org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1180)
    org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:950)
    org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:852)
    org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:882)
    org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:778)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:728)

Would you please help me?
And how to write an array in 'spring jsp view'?
Thank You...

3
  • YOu have not defined what rst is in your JSP .. Please do that and it should work .. Commented Mar 22, 2014 at 9:30
  • I have been searching how to define it and I can't find it. Would you please give me an example how to define it? Commented Mar 22, 2014 at 10:40
  • My answer shows how. Hope this helps. Commented Mar 22, 2014 at 10:45

1 Answer 1

1

Add this to the JSP

 <% ResultSet rst = (ResultSet)request.getAttribute("resultSet");%>
 <%= rst.getString("resultSet") %>

Change your pasar method to

model.addAttribute("resultSet",rst);
Sign up to request clarification or add additional context in comments.

1 Comment

I have change my method but it still doesn't work. Its give me a notification error on the line <% ResultSet rst = [...] -> "The method getAttribute(String) in the type ServletRequest is not applicable for the arguments()"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.