How to create JSP response without hardcoding the HTML in java code

HTML lines can be hard-coded in the Java Class, but… this technique is not advised. In the MVC Architecture, The View component must be separated from the Controller component. And JSP views must be separate for easy editing, No need to compile the Java Class when there is a modification.

The technique to do this is using the following

1.) set request.setAttribute()this binds the variable to be displayed in the View as set in the Java Controller Class.

2.) In the JSP View, create the ${variable} to accept the input from the Controller Class

3.) To create the response, do request.getRequestDispatcher("/<response>.jsp").forward(request, response);

Where <response> is the JSP file that displays the variables.

It is explained more in this links:

http://stackoverflow.com/questions/6452537/servlet-send-response-to-jsp

http://stackoverflow.com/tags/servlets/info

Advertisements

One thought on “How to create JSP response without hardcoding the HTML in java code

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s