aspose file tools*
The moose likes Servlets and the fly likes Servlet vs JSP Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Servlet vs JSP" Watch "Servlet vs JSP" New topic
Author

Servlet vs JSP

Yatish Sonkeshariya
Greenhorn

Joined: Sep 28, 2011
Posts: 16

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?

Thanks, Yatish Sonkeshariya
Pune(411001), India
Tim Moores
Rancher

Joined: Sep 21, 2011
Posts: 2408
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

Joined: Sep 28, 2011
Posts: 16

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
Rancher

Joined: Sep 21, 2011
Posts: 2408
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

Joined: Oct 07, 2009
Posts: 24

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

Joined: Nov 14, 2011
Posts: 13
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

Joined: Oct 11, 2011
Posts: 98
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
Bartender

Joined: Oct 14, 2005
Posts: 18906
    
    8

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

Joined: Jan 10, 2002
Posts: 61653
    
  67

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.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61653
    
  67

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

Joined: Aug 11, 2007
Posts: 4659
    
    5

Listen to @Bear, he is correct, plus he looks like a walrus
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61653
    
  67

Pat Farrell wrote:plus he looks like a walrus

Which is always a sign of trustworthiness!
Alok kumar Ranjan
Greenhorn

Joined: Nov 24, 2011
Posts: 1

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

Joined: Jan 10, 2002
Posts: 61653
    
  67

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.
 
 
subject: Servlet vs JSP