aspose file tools*
The moose likes Servlets and the fly likes How to convert a servlet into jsp. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "How to convert a servlet into jsp." Watch "How to convert a servlet into jsp." New topic
Author

How to convert a servlet into jsp.

Deepa Kulkarni
Greenhorn

Joined: Feb 20, 2012
Posts: 6
I have to use jsp instead of servlet in my application.Can you please help me to get this..Thanks
Tim Moores
Rancher

Joined: Sep 21, 2011
Posts: 2408
Usually, servlets and JSPs are used in conjunction; using just one is just as bad as using just the other. Can you tell us what the circumstances are?
Deepa Kulkarni
Greenhorn

Joined: Feb 20, 2012
Posts: 6
Hi Tim,
The servlet that I am using in my application returns me HTML which shows calender structure. Now I have to replace that servlet with jsp that means I have to add the java code of servlet (returns calender in HTML fromat) into new jsp.
Tim Moores
Rancher

Joined: Sep 21, 2011
Posts: 2408
Java code in JSPs is very bad practice, and not necessary. You should use a servlet that contains all Java code, and which adds request attrbutes with all data that the JSP requires for display. The request is then forwarded to the JSP which proceeds to use the request attrbutes to generate the HTML.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61654
    
  67

Tim is quite correct. Putting JAva code into a JSP is a practice that has been discredited for 10 years now. In moderne JSPs, the JSTL and EL are used in place of bad-practice Java code.

You might find this article helpful in learning how to structure moderne web applications.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18907
    
    8

Likewise servlet code which outputs HTML is a bad thing, which hopefully is why you've been told to change this servlet.

So you're going to have to redesign. That means you will have to look at the servlet and figure out which part is logic needed to set up data, and which part is writing that HTML. Quite likely you will find that the two parts are intermingled. Then you will have to rewrite the code so that the "logic needed to set up data" is retained in the new version of the servlet, with changes made so that the data is put into request-scoped variables rather than in local Java variables in the doGet method. And you will need to write a new JSP which writes out the desired HTML, using the scoped variables which the servlet created.

In other words, it's basically a rewrite. Think of it as hitting the original code with a big hammer, then taking the debris and putting the "servlet" bits into one pile and the "JSP bits into another pile, then making the two piles into useful code. Not necessarily an easy task.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61654
    
  67

Well put, Paul. Very well put.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to convert a servlet into jsp.