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: 2407
posted
0
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?
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: 2407
posted
0
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
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
posted
0
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
posted
0
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.
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.