passing servlet content to client via json.

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

LVL 12
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.