Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: Java Servlet
following is the code to show data from servlet to jsp,
 
---------------------------------
in the browser : uri:"http://localhost:8080/shikkha/WEB-INF/classes/se/simpleservlet.java"
-----------------------------------
HTTP Status 404 -<small></small>
 
type Status report
 
message
 
description The requested resource () is not available.
 
Apache Tomcat/7.0.12
------------------------------------------------------------------------------------------------
code:
-----------------------------------------------------------------------------------------------
package se;
 
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
 
import javax.servlet.RequestDispatcher;
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.rdf.model.Property;
import com.hp.hpl.jena.rdf.model.Resource;
import com.hp.hpl.jena.rdf.model.Statement;
import com.hp.hpl.jena.rdf.model.StmtIterator;
import com.hp.hpl.jena.util.FileManager;
import com.hp.hpl.jena.util.PrintUtil;
 
/**
 * Servlet implementation class simpleservlet
 */
@WebServlet("/simpleservlet")
public class simpleservlet extends HttpServlet {
    private static final long serialVersionUID = 1L;
    public static final String MY_FOAF_FILE = "//home/ubuntu/indoor.rdf";
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public simpleservlet() {
        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
        response.setContentType("text/html");
    }
 
    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 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);
			 
        PrintWriter writer = response.getWriter();
        writer.println("<html><body>");
        Resource s=null; Property p=null; Resource o=null;
 
        writer.println("</body></html>");
 
        ArrayList<rowobject> resultss = new ArrayList<rowobject>();
 
        while ( results.hasNext()) {
            RowObject result = new RowObject();
            QuerySolution binding = results.nextSolution();
            result.setName(binding.get("object"));
            resultss.add(result);
            System.out.println(" - " + result.getName().toString());
        }
 
        request.setAttribute("resultss", resultss); // Will be available as ${results} in JSP
        try {
            //request.getRequestDispatcher("/wow.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();
        }
    }
}
Posted 29-Mar-13 8:27am
Edited 29-Mar-13 11:51am
v2
Comments
Monster Maker at 29-Mar-13 18:11pm
   
I think, something is wrong with deployment descriptor!
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Hello,
 
Try changing the @webservlet annotation as shown below if you want your servlet to request with an extra path
@webservlet(urlPatterns = {"/simpleservlet/*"})
OR as shown below to listen to exact path
@webservlet(urlPatterns = {"/simpleservlet"})
Also make sure the webapp's web.xml has been declared to conform Servlet 3.0 spec.
<web-app 
    xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
    version="3.0">
Regards,
  Permalink  
v3
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

i have changed
 

but showing same error, and in the changed line it is saysing
 
Multiple markers at this line
- webservlet cannot be resolved to a type
- The attribute urlPatterns is undefined for the annotation type
  Permalink  

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



Advertise | Privacy | Mobile
Web01 | 2.8.1411022.1 | Last Updated 29 Mar 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