Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: Java Servlet
the jsp code:
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<!-- Create Bean Instance-->
<jsp:useBean id="RowObject" class="finalpackage.RowObject" scope="session"></jsp:useBean>
 
<jsp:setProperty property="*" name="RowObject"/>
 
<c:forEach var="RowObject" items="${RowObject.name}">
<option>
<c:out value="${RowObject}" />
</option>
</c:forEach>
servlet call-----------------
package finalpackage;
 
import java.util.*;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
 

import com.hp.hpl.jena.query.QueryExecution;
import com.hp.hpl.jena.query.QueryExecutionFactory;
import com.hp.hpl.jena.query.QueryFactory;
import com.hp.hpl.jena.query.QuerySolution;
import com.hp.hpl.jena.query.ResultSetFormatter;
import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.ModelFactory;
import com.hp.hpl.jena.util.FileManager;
 

/**
 * Servlet implementation class smartservlet
 */
@WebServlet("/smartservlet/")
//@webservlet(urlPatterns = {"/smartservlet/*"})
public class smartservlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	public static final String MY_FOAF_FILE = "//home/ubuntu/indoor.rdf";
    /**
     * @see HttpServlet#HttpServlet()
     */
    public smartservlet() {
        super();
        // TODO Auto-generated constructor stub
    }
 
	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(request, response);
	}
 
	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		// TODO Auto-generated method stub
				response.setContentType("text/html");
				// TODO Auto-generated method stub
							
				Model model = ModelFactory.createDefaultModel();			
 
				
			    FileManager.get().readModel(model,MY_FOAF_FILE);
			    model.read(new File(MY_FOAF_FILE).toURI().toString());
			    String queryString =  "SELECT ?predicate ?object   WHERE {?subject ?predicate ?object .}";  
				
			    com.hp.hpl.jena.query.Query query = QueryFactory.create(queryString);
			     QueryExecution qe = QueryExecutionFactory.create(query, model);
 
			     	com.hp.hpl.jena.query.ResultSet results = qe.execSelect();
			     	
					 ResultSetFormatter.out(System.out, results);
					
					 		 
					    		
					    		
					 List<rowobject> resultss = new ArrayList<rowobject>();
					 	
				      while ( results.hasNext()) {
				    	  System.out.println("check 1");
				     RowObject result = new RowObject();
				     QuerySolution binding = results.nextSolution();
				     result.setName(binding.get("object").toString());
				      resultss.add(result);
				    
				
				      }
					      
					     String dbs="ame";
					    request.setAttribute("resultss", resultss); // Will be available as ${results} in JSP
					 //  request.setAttribute("dbs", dbs); // Will be available as ${results} in JSP
					         try {
									request.getRequestDispatcher("/woo.jsp").forward(request, response);
									
									//RequestDispatcher RequetsDispatcherObj =request.getRequestDispatcher("/wow.jsp");
									//RequetsDispatcherObj.forward(request, response);
								} catch (ServletException e) {
									// TODO Auto-generated catch block
									e.printStackTrace();
								} catch (IOException e) {
									// TODO Auto-generated catch blockl
									e.printStackTrace();
								}
	}
 
}
the bean
--------------------
package finalpackage;
 
    public class RowObject {
 

 
   public String name;
    /**
     * @return the name
     */
    public String getName() {
        return name;
    }
    /**
     * @param name the name to set
     */
    public void setName(String name) {
        this.name = name;
    }
    }
Posted 29-Mar-13 21:31pm
Edited 29-Mar-13 22:32pm
v2

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Hello,
 
First I think jsp:setProperty for RowObject is not required. Secondly you are populating a list of RowObject in Servlet and setting it in request with key as 'resultss'. So in JSP you should be iterating over resultss. The corret JSP code would be
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Your page title</title>
</head>
<body>
<table>
<c:forEach var="rowVal" items="${requestScope.resultss}">
    <tr>
        <td><c:out value="${rowVal.name}"/>
    </tr>
</c:forEach>
</table>
</body>
</html>
Another change would be in the doPost method of your servlet as shown below.
    RequestDispatcher rd = null;
    try {
            rd = request.getRequestDispatcher("/woo.jsp");
 
    } catch (ServletException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch blockl
        e.printStackTrace();
    }
    request.setAttribute("resultss", resultss); // Will be available as ${resultss} in JSP
    rd.forward(request, response);
  Permalink  
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 410
1 Jochen Arndt 200
2 Richard MacCutchan 135
3 DamithSL 105
4 PIEBALDconsult 90
0 OriginalGriff 6,045
1 DamithSL 4,601
2 Maciej Los 4,032
3 Kornfeld Eliyahu Peter 3,480
4 Sergey Alexandrovich Kryukov 3,220


Advertise | Privacy | Mobile
Web01 | 2.8.141220.1 | Last Updated 2 Apr 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100