• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Servlet vs JSP

 
Yatish Sonkeshariya
Greenhorn
Posts: 16
Hibernate Java Spring
  • 0
  • Mark post as helpful
  • send pies
  • 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?
 
Tim Moores
Bartender
Pie
Posts: 2488
4
  • 0
  • Mark post as helpful
  • send pies
  • 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 Java Spring
  • 0
  • Mark post as helpful
  • send pies
  • 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
Pie
Posts: 2488
4
  • 0
  • Mark post as helpful
  • send pies
  • 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
 
Akhilesh Murthy
Greenhorn
Posts: 24
Java
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
Abhish Sharma
Greenhorn
Posts: 13
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
Miku Ranjan
Ranch Hand
Posts: 98
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
Paul Clapham
Sheriff
Pie
Posts: 20166
24
MySQL Database
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64173
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • 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
Author and ninkuma
Marshal
Pie
Posts: 64173
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • 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?
 
Pat Farrell
Rancher
Posts: 4660
5
Linux Mac OS X VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Listen to @Bear, he is correct, plus he looks like a walrus
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64173
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pat Farrell wrote:plus he looks like a walrus

Which is always a sign of trustworthiness!
 
Alok kumar Ranjan
Greenhorn
Posts: 1
Eclipse IDE Hibernate Spring
  • 0
  • Mark post as helpful
  • send pies
  • 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
Author and ninkuma
Marshal
Pie
Posts: 64173
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • 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.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic