aspose file tools*
The moose likes Servlets and the fly likes init() method? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "init() method?" Watch "init() method?" New topic
Author

init() method?

Bryan Scarbrough
Ranch Hand

Joined: Aug 08, 2005
Posts: 49
I have a servlet class that I am modifying to use as a bean with a JSP page and this servlet uses the init() method for a set of code to connect to a database when the class is called.

My question is, will the init() method still be called even though the class is no longer being used as a servlet?

If not, how can I make it do that?

Thanks in advance!


Bryan Scarbrough<br /> <br />Consistency is the last resort of the unimaginative!
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60082
    
  65

I have a servlet class that I am modifying to use as a bean with a JSP page


Can you explain why you would want to do such a thing?


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Bryan Scarbrough
Ranch Hand

Joined: Aug 08, 2005
Posts: 49
Because currently I am restricted to a JavaScript form validation for data entry into the database system and I did not think that you could use Javascript inside a servlet.

If this is possible, then by all means, please let me know how to do it!!!

I would love to use servlets instead of "clunky" jsp tags and scriptlets.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 39578
    
  27
A few points:

- You're right, JavaScript is client-side, servkets/JSPs/beans are server-side, and never the twain shall mix

- a bean doesn't have an init method, but it does have a constructor in which you could perform initialization work

- but the big questions is: what do you think you can do in a bean that you couldn't do in the servlet? There's both Java code, and neither is in the form of scriptlets (which reside in JSP pages).


Ping & DNS - updated with new look and Ping home screen widget
Bryan Scarbrough
Ranch Hand

Joined: Aug 08, 2005
Posts: 49
This is a project for college and I am required to create a web app that ties together several different aspects of what we have learned in previous classes. This project it very heavily reliant on Java and J2EE concepts, but it also requires some aspects to be in PHP, Python or Javascript to show the understanding of those concepts as well.

It was my initial thought to use Javascript for client side processing of form data, etc. and then to use JSP/JavaBeans to take the data and insert into/extract from a MySQL database. I am quickly discovering that there is more that I have yet to learn than what I thought I already knew!!

Anyway, I am much better in the other languages than I am in Java or JavaScript, but I cannot determine a distinct advantage of using either PHP or Python as opposed to JavaScript. Thus, I am pretty well frustrated in trying to sort through JSP and figure out how to take all of the servlet apps that I have and convert them to some compatible form.

If there is a better way (and at this point, I am pretty sure there is - though I do not have a clue what that would be) then please feel free to give your 2 cents worth. At this point I am ALL ears!!

Thanks for your consideration and help and for listening to me ramble on about my frustrations...
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 39578
    
  27

but I cannot determine a distinct advantage of using either PHP or Python as opposed to JavaScript. Thus, I am pretty well frustrated in trying to sort through JSP and figure out how to take all of the servlet apps that I have and convert them to some compatible form.


Don't compare JavaScript to PHP or Python. PHP/Python are server-side technologies (like servlets and JSPs), while JavaScript is client-side. They can be used together.
Servlets and JSPs are largely equivalent from a technology point of view (JSPs are often implemented by converting them to servlets), so what can be done with one, can generally be done with the other as well. The difference lies in architecural and programming convenience considerations.
Good luck with the project.
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Originally posted by Bryan Scarbrough:


If there is a better way (and at this point, I am pretty sure there is - though I do not have a clue what that would be) then please feel free to give your 2 cents worth. At this point I am ALL ears!!


I would read up on the Model, View, Controller (MVC) design pattern.
Using MVC, you could move all of you database handling into a bean, all of your request handling into a servlet, and all of your view code (including client side Javascript validation) to the JSP.

I have an example MVC app that you can use as a starting point if you like.
http://simple.souther.us
Look for SimpleMVC.


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Bryan Scarbrough
Ranch Hand

Joined: Aug 08, 2005
Posts: 49
Ulf, I was never comparing JavaScript with PHP/Python. Quite the contrary! It is actually their differences that are making it difficult to utilize one of those technologies in lieu of JavaScript. I understand that they are VERY different in principle, usage and capability. The main problem I guess I am having is the conversion of servlets to JSP's, or where to use which technology.

Thanks Ben for your reply as well, I am really looking forward to looking at some of your implementation details. I have never looked at MVC, so I am looking forward to checking it out.

Well, it is off to headacheville I go now to try to figure all of this out and still be able to write all of my papers too!! Oh, well...
Yuriy Zilbergleyt
Ranch Hand

Joined: Dec 13, 2004
Posts: 429
Just keep in mind that it's very easy to use both JavaScript together with the server-side technologies - either by keeping it in a seperate .js file that the web page includes, or by actually outputting the JavaScript code using JSP or PHP, just like you would output HTML.

-Yuriy
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: init() method?
 
Similar Threads
jspInit()
Mechanism of init method to initialize servlet
Why do we have init() method in Servlets ?
sinificance of value of tag <load-on-startup> in web.xml
when init() method is called ?