• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Liutauras Vilda
  • Paul Clapham
Sheriffs:
  • paul wheaton
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Piet Souris
Bartenders:
  • Mike London

Servlet vs JSP

 
Greenhorn
Posts: 16
Hibernate Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello All,
I have some doubts related to Servlet and JSP.

JSP automatically gets converted into Servlet, also we can write the java code in jsp so can we write the processing code in jsp to handle the request completely?
Writting a servlet for each jsp is mandatory or not?
 
Bartender
Posts: 7488
171
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

can we write the processing code in jsp to handle the request completely?


You can, but you absolutely should not. Java code has no place in JSPs. That style of web development has gone out of fashion 10 years ago. Read up on the MVC style of developing web apps.

Writting a servlet for each jsp is mandatory or not?


No
 
Yatish Sonkeshariya
Greenhorn
Posts: 16
Hibernate Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Tim,
Thanks for quick reply.

Suppose i have 1 jsp page and i want to handle doGet() and doPost() in that jsp page itself then how i'll override associate method in jsp and where we place the java code in JSP?
Why we should not to write java-code in jsp and wht dou you mean by "Java code has no place in JPSs" ?
 
Tim Moores
Bartender
Posts: 7488
171
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
These days (actually, more like for the last 10 years) industry consensus is that a separation of Model, View and Controller makes for a much better design than to mix code and HTML. Read up on MVC to learn about it. So, no, I'm not going to talk about how to achieve what you're asking, because it's the wrong thing to do. There was a fun article about that right here: http://www.javaranch.com/journal/200603/Journal200603.jsp#a5
 
Ranch Hand
Posts: 31
1
Oracle Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
JSP comes into picture when you want to display some dynamic content on your web page. For all your business logic must be placed in the Model layer.
 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Yatish Sonkeshariya wrote:Hello Tim,
Why we should not to write java-code in jsp and wht dou you mean by "Java code has no place in JPSs" ?



Java code should not be written in JSP simply because when you need to change the code , this becomes quite hectic. Apart from that code get engraved in JSP and MVC architecture gives you rather more flexibilty. So that when you need to change some portion of code like adding or removing some field you need not to scam all the java files to add the associated document.
MVC architecture clearly distinguished Model,controller and view.
 
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I am also agree that in jsp we should should write java although we can write but for a proper MVC application we should avoid it just use minimum java code in jsp.
 
Marshal
Posts: 27533
88
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Yatish Sonkeshariya wrote:Suppose i have 1 jsp page and i want to handle doGet() and doPost() in that jsp page itself then how i'll override associate method in jsp...?



If you want the GET and POST methods to be handled by the same piece of code on the server, then your web design is wrong. The design of a request should include the HTTP method it uses; there are different reasons to use GET and POST. So don't start writing your code before that design has been done.
 
Sheriff
Posts: 67694
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Miku Ranjan wrote:I am also agree that in jsp we should should write java


I'm sure you meant that we should not write Java into a JSP. Ever.
 
Bear Bibeault
Sheriff
Posts: 67694
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Yatish Sonkeshariya wrote:Suppose i have 1 jsp page and i want to handle doGet() and doPost() in that jsp page itself


Then you are doing it wrong. No submissions should ever be made to a JSP. A servlet should be used to process incoming data.

Did you read the article that Tim linked to?
 
Rancher
Posts: 4803
7
Mac OS X VI Editor Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Listen to @Bear, he is correct, plus he looks like a walrus
 
Bear Bibeault
Sheriff
Posts: 67694
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Pat Farrell wrote:plus he looks like a walrus


Which is always a sign of trustworthiness!
 
Greenhorn
Posts: 1
Hibernate Eclipse IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i think it is not mandatory to write the servlet code for each jsp page .It induce by default.
 
Bear Bibeault
Sheriff
Posts: 67694
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Alok kumar Ranjan wrote:i think it is not mandatory to write the servlet code for each jsp page .It induce by default.


The servlet code that is generated from the JSP is automatically created. The servlet code for the page controller for the JSP is not.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic