Java servlet DOM manipulation and html output -
i trying html elements id in java servlet, , change content , display document. problem got elements , set them (atleast think so), how display in browser, here 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); (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(); } } } }
} there problem, or supposed display html file, set attributes ?
well - need serialize document , send browser. far have code reads file disk , manipulates in memory. in order send browser need serialize , write serialized representation browser using printwriter response.getwriter().
so basic problem servlet doesn't send client.
see this answer details of how implement missing step.
Comments
Post a Comment