Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to convert a servlet into jsp.

 
Deepa Kulkarni
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have to use jsp instead of servlet in my application.Can you please help me to get this..Thanks
 
Tim Moores
Bartender
Posts: 2733
36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Bartender
Posts: 2733
36
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64700
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Paul Clapham
Sheriff
Pie
Posts: 20955
31
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64700
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well put, Paul. Very well put.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic