passing servlet content to client via json.

Posted on 2013-09-09
Medium Priority
Last Modified: 2013-09-26
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='//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js'></script>");
		out.println("<script src='js/bootstrap.min.js'></script>");



Open in new window

Question by:jazzIIIlove
LVL 83

Accepted Solution

leakim971 earned 1000 total points
ID: 39475924
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
LVL 27

Assisted Solution

mrcoffee365 earned 1000 total points
ID: 39477528
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.

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Introduction Knockoutjs (Knockout) is a JavaScript framework (Model View ViewModel or MVVM framework).   The main ideology behind Knockout is to control from JavaScript how a page looks whilst creating an engaging user experience in the least …
In this post we will learn different types of Android Layout and some basics of an Android App.
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
Suggested Courses

624 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question