passing servlet content to client via json.

jazzIIIlove used Ask the Experts™
Hi there;

I have a working code which works properly but I want it more efficient and use JSON to pass the content to the client. I have some background with Jackson.

So, practically, how can i convert the following code in a feasible case to form JSON and print out in the client screen?

public class UploadPageGenServlet extends HttpServlet {
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		PrintWriter out = response.getWriter();

		// String userName = request.getParameter("userName");
		String userName = (String) request.getSession()
		out.println("<!DOCTYPE html>");

		out.println("<link href='css/bootstrap.min.css' rel='stylesheet' media='screen'>");
		out.println("<link href='css/your-css.css' rel='stylesheet'>");

		out.println("Welcome " + userName);
		out.println("<br />");
		RequestDispatcher rd = request.getRequestDispatcher("uploadPage");

		out.println("<h3>File Upload:</h3>");
		out.println("<br />");
		out.println("Select a file to upload: ");
		out.println("<!-- this form is mulitpart encoded for using post as submission method for file uploads -->");
		out.println("<form  id='uploadform' action='" + userName
				+ "/postJob'"  + "method='post' enctype='multipart/form-data'>");
		out.println("<input type='file' name='file' size='50' />");
		out.println("<br /> <br/>");
		out.println("<!-- posts to uploadServlet and invokes doPost, this is mapped in web.xml -->");
		out.println("<input type='submit' value='Upload File' />");
		out.println("<br /> <br/>");
		out.println("<!--  this form is used for listing the jobs, separate form needed as other form is post and multipart, this form is get -->	");
		out.println("<form id='listform' action='" + userName
				+ "/listJobs' method='get'  >");

		out.println("<input type='submit' value='List Jobs' />");
		out.println("</form>	");		
		out.println("<a href='LogoutServlet' accesskey='1'>Logout</a>");

		out.println("<script src='//'></script>");
		out.println("<script src='js/bootstrap.min.js'></script>");



Open in new window

Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Top Expert 2014
How are you calling : UploadPageGenServlet

The only variable here is username so why do you want to use JSON to transport HTML?

you just need to load with Ajax (or redirect) to an HTML page and just request the username by ajax and you're done

there's NO reason to put your HTML content in JSON data
Top Expert 2007
leakim971 is right.  Very right, in fact.  The small savings from not returning html from UploadPageGenServlet  will be far overshadowed by the transfer of files in the file upload, so you won't see any performance improvement by changing your interaction to not receive the html of the file upload page from UploadPageGenServlet .

If you don't want the html for the upload form to be returned, then the html has to exist in the page which is running UploadPageGenServlet  and you will use Ajax (?) to return the values from the form to the server?

If that's what you're looking for, then you'll need a div in the existing page (or all pages) which have the file upload form, and then collect the field values to send to the server with Ajax.  Then the Ajax javascript will need to receive the response from the server and do the next thing -- maybe display "files uploaded" in the page?

If the html for the upload form is all in the page which currently posts to UploadPageGenServlet  then you can change UploadPageGenServlet  to only return whatever values Ajax will read and display as the result to the user.  Which could be in JSON format if you want.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial