• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

init() method?

 
Bryan Scarbrough
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65128
92
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Bryan Scarbrough
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).
 
Bryan Scarbrough
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Bryan Scarbrough
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 429
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic