java - Updating dynamically jsp page in browser after POST http request -
i have following project: backend written in java; main class creates listener on log file, retrieves info when log modified, , creates object called "candidate", , send of attributes servlet via post method. works until here, , 200 response code servlet.
then, created frontend (with bootstrap such looks nice) in index.jsp file, consist of static part (background, messages, logos, etc.) , dynamic part, want update each time log file modified. simplified version of index.jsp can found below, in firstname example of data should arrive post request:
<%@ page language="java" contenttype="text/html; charset=iso-8859-1" pageencoding="iso-8859-1"%> <!doctype html public "-//w3c//dtd html 4.01 transitional//en" "http://www.w3.org/tr/html4/loose.dtd"> <html> <head> <meta http-equiv="content-type" content="text/html; charset=iso-8859-1"> <!-- if ie use latest rendering engine --> <meta http-equiv="x-ua-compatible" content="ie=edge"> <!-- set page width of device , set zoon level --> <meta name="viewport" content="width = device-width, initial-scale = 1"> <title>user page</title> <style> @import url('bootstrap/css/bootstrap.css'); </style> <script type=”text/javascript” src=”bootstrap/js/bootstrap.min.js”></script> </head> <body style="background-image:url(images/xxxx.jpg); background-size:cover; " > <% string firstname = (string) request.getparameter("firstname"); %> <div class="container"> <div class="page-header"> <h1 style="color:white">hello <%if(firstname != null){%> <%=firstname %> <%}%> welcome new page.</h1> </div> <img src="images/xx.png" style="width:35%;height:35%; padding-left: 10%; padding-top:10%"/> </div> </body> </html>
my ultimate goal user sees index.jsp page, updates of content data "new candidate" each time log modified.
to run application, have several run instances. run main class, , on top of that, run index.jsp , myservlet on server. right way run it?
because when that, , modify log, main class console prints everything, showing post request has been sent (i have tried send request servlet or directly jsp file) no request received index.jsp (or @ least doesnt display in browser opened in eclipse), after refreshing page.
what work, however, when modify manually url in browser add url query parameter ?firstname=xxxx
, refresh, message included in index.jsp page. how can make sure sent post request through backend dynamically modifies index.jsp page?
last note: when log file modified, console prints system.out.println corresponding first name seen below in doget method, how assumed works there.
here servlet code:
package com.xyz.xxxxx; import java.io.ioexception; import java.io.printwriter; import javax.servlet.servletcontext; 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 javax.servlet.http.httpsession; /** * servlet implementation class myservlet */ @webservlet("/myservlet") public class myservlet extends httpservlet { private static final long serialversionuid = 1l; /** * @see httpservlet#dopost(httpservletrequest request, httpservletresponse response) */ protected void doget(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception { //response.setintheader("refresh", 5); response.setcontenttype("text/html"); printwriter writer = response.getwriter(); string firstname = request.getparameter("firstname"); system.out.println("testing get: first name : " + firstname); // forward request jsp page request.getrequestdispatcher("/web-inf/index.jsp").forward(request,response); } protected void dopost(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception { doget(request,response); } }
and file organization here.
thank !
i'm going take leap of faith here , assume looking persist values of firstname
post
this require adjust code quite bit, don't want provide implementation if classroom project.
from code snippet you've provided: looks trying achieve following:
- get -> display index.html name supplied post
- post -> change name given session.
therefore, should accessing page using whatever defined on servletclass.
once understand httpsession, should desired effect
Comments
Post a Comment