0

I am trying to get html elements by ID in java servlet, and change their content and then display the document. My problem is I got the elements and set them (atleast I think so), but now how to display it in browser, here what I have done :

@WebServlet(description = "profile page", urlPatterns = { "/profile/*" })
public class RouteServlet extends HttpServlet {
private static final long serialVersionUID = 1L;

protected void doGet(HttpServletRequest request,
        HttpServletResponse response) throws ServletException, IOException {
    response.setContentType("text/html");
    String uri = request.getRequestURI();
    final String start = "/social/profile/";
    String userId = uri.substring(start.length());
    long id = Long.parseLong(userId);
    //response.getWriter().print(id);
    for (Info j : InfoRegistry.getInstance().getInfoList()) {
        if (j.getId() == id) {
            File template = new File("profile-template.html");
            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
            DocumentBuilder builder;
            Document doc;
            try {
                builder = factory.newDocumentBuilder();
                doc = builder.parse(template);
                doc.getDocumentElement().normalize();
                doc.getElementById("head").setTextContent(j.getName());
                doc.getElementById("name").setTextContent(j.getName());
                doc.getElementById("birth")
                        .setTextContent(j.getBirthDate());
                doc.getElementById("from").setTextContent(j.getCountry());
                doc.getElementById("desc").setTextContent(
                        j.getDescription());
                doc.getElementById("mail").setTextContent(j.getEmail());


            } catch (ParserConfigurationException e) {

                e.printStackTrace();
            } catch (SAXException a) {

                a.printStackTrace();
            }

        }
    }

}

} So is there any problem, or what I am supposed to do to display the html from the file, with the now set attributes ?

2 Answers2

0

Well - you need to serialize the document and send it back to the browser. So far what you have is some code that reads a file from disk and manipulates it in memory. In order to send this to the browser you need to serialize it and write the serialized representation back to the browser using the PrintWriter you get from response.getWriter().

So the basic problem is that your servlet doesn't send anything back to the client.

see this answer for the details of how to implement the missing step.

Community
  • 1
  • 1
Woody
  • 7,578
  • 2
  • 21
  • 25
0

I do not recommend to do that from a servlet, you should make an Ajax Request from the browser to get some data, and then, manipulate the client HTML via JQuery o simple javascript, but if you decide to use that way, I recommend to serialize your document and then send it to your client via PrintWritter. I prefer to use POJO Mapping features to support JSON responses

AlbertoRuvel
  • 356
  • 4
  • 14