CodyBugstein
CodyBugstein

Reputation: 23322

Programmatically Display a web page from a Servlet class

I'm really new to JSP and Servlets and all that jazz, and I'm confused about how to approach it.

Right now my main confusion is as follows:

It seems there are two ways to get a web page to display on the screen in a Java EE/JSP project:

  1. Create an HTML page with the extension .jsp and in the web.xml file, map it to a url pattern (let's assume just /)
  2. Create a Java class that extends Servlet, override the doGET() method to return a string of HTML code and map to this Java class in the web.xml.

My JSP project requires a decent amount of Java code to perform logic and login/logout operations. I've read that it's bad practice to inlcude Java code inside JSP pages as it becomes impossible to reuse, hard to keep track of, messy etc. I want to include much of code in Java, which will feel much more natural for me.

How should I build my project (assuming it's a simple beginner project in which I want to employ the best practices for organization, testing etc in preparation for building a larger project)? How can cleanly deploy web pages from inside Java classes, rather than having many JSP pages containing bits of Java code?

Upvotes: 2

Views: 2409

Answers (5)

nschroyer
nschroyer

Reputation: 11

I'm not sure where you heard that its bad practice to include Java code in the HTML(JSP) files but in order to have a dynamically data driven website, Java is what you would use in the HTML(JSP). Keep in mind, all of your front end design will be in the HTML using JSP, and all of the your logic for handling requests and redirectors and passing data will be managed in the servlets using Java.

Your servlets can pass data, whether it be from a database or other data source, to the front end and your JSP will just help in displaying that data. It's called JSP for a reason. You can write Java in it.

The best approach for designing websites in Java is to use the MVC pattern that way it helps seperate all of the core functions of the site and can easily be scaled and managed.

Do you have any code that you have to show what you have done so far?

Upvotes: 0

joey rohan
joey rohan

Reputation: 3566

To manage bit of java code, you can use Scriptlet.

Also mentioned in other answers, you can use jsp:forward or response.sendRedirect to call web pages.

Also see What is the difference between jsp:forward and response.sendRedirect

Upvotes: 0

Serge Ballesta
Serge Ballesta

Reputation: 148900

If your are looking for best practices in clean separation of java code from the presentation, you might as well use a MVC Framework (Spring MVC or Struts are well know examples) that will help you to cleanly separate :

  • view layer (JSP)
  • controller layer
  • service layer
  • domain objects
  • persistence layer

It may be hard to begin with, but you will find nice tutorials through official sites and google.

If you want to only use servlets and JSP, the common rule is to have all logic and Java code in servlets, and that those servlets simply forward to JSP pages that will do the view part. The communication between then is done with request attributes (and of course, session and application context attributes.

Good luck ...

Upvotes: 0

Chris Gerken
Chris Gerken

Reputation: 16392

The pattern I've used in the past is to:

1) Write the servlet to perform whatever logic is necessary and to construct a reasonable number of serializable java objects to hold all of the data you want to be rendered via HTML.

2) The servlet stores those objects into the HTTP request and forwards the request and response objects to a JSP:

request.getRequestDispatcher("/some.jsp").forward(request, response);

3) The JSP can access the java objects in the request and merge the data with the static HTML to complete the request.

Upvotes: 1

dimoniy
dimoniy

Reputation: 5995

This is exactly the problem MVC pattern solves.

You do all your complex logic in the Controller (simple Java class, or a Servlet), then pass the data to View (JSP, Velocity, Freemarker, anything that can process the data and make a HTML).

There are several implementations of MVC for Java most popular being Spring MVC and Struts. Alternatively, you can just use your servlet for all your logic and then manually forward the request to a JSP for processing:

request.getRequestDispatcher("/index.jsp").forward(request,response);

Upvotes: 0

Related Questions