0

I have written the code in java. All the things are working properly. I just want to return the value of modelandview that is not returning correct web page. It is throwing blank page. The code of my controller is:

public class Part3 extends HttpServlet {

Num num = new Num();
String name[];
Record record = null;

public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    ModelAndView mv = new ModelAndView();
    String number = request.getParameter("number");
    num.setNumber((number));

    Driver driver = GraphDatabase.driver("bolt://localhost", AuthTokens.basic("neo4j", "neo4j"));
    Session session = driver.session();
    StatementResult result = session.run("MATCH (tom {name: '" + number + "'}) RETURN tom.name, tom.born, tom.roles;");
    while (result.hasNext()) {
        record = result.next();
    }
    session.close();
    driver.close();

    request.setAttribute("selectObject", record);
    mv.setViewName("index");
    return mv;
}

public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException {
    try {
        handleRequest(request, response);
    } catch (IOException ex) {
        Logger.getLogger(Part3.class.getName()).log(Level.SEVERE, null, ex);
    }
}
}

My HTML page is index.html and its code is:

<form action ="number" name ="number">
            <b>Enter Song ID:</b>
            <input type="text" name='number' required  placeholder="Enter SongID"><br /><br /><br />
            <input type="submit"  value="Submit" class="btn btn-primary" style="background-color: orangered"/> 
        </form>
        ${selectObject}
    </div>
Uday Verma
  • 99
  • 9
  • To me, it looks like a mix of plain Servlet programming and Spring MVC. I'm pretty sure, it ain't gonna work like that. If you want to use ModelAndView, it's a DispatcherServlet you have to register and not your servlet, and you must ensure that DispatcherServlet knows your controller, i.e. the class that holds the handleRequest method. – Arthur Noseda Aug 07 '16 at 09:43

1 Answers1

0

ModelAndView is a spring mvc object. You cannot use it in a servlet.

Your servlet is doing nothing, in a servlet you need to write to the object HttpServletResponse. What do you try to do with this code handleRequest(request, response); ?

update :

First, In your object reponse, you can get a PrintWriter : response.getWriter(). Write response in this writer.
Second, delete ModelAndView, you don't need it. Try to understand what is a servlet before using Spring MVC.

Here is a simple example : http://www.tutorialspoint.com/servlets/servlets-first-example.htm

Bilal BBB
  • 1,154
  • 13
  • 20