Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

using model 1 Architecture for inserting row into DB...  RSS feed

 
Kalyana Sundaram
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The question is on the design of a JSP using Model 1 architecture.

I have a html form with 3 fields taking input from user. I have made a java bean with 3 instance varibales in the name of the field names and with getter() setter() methods too.

I dont want to hardcode the database access in the JSP. How do i acheive modularity or make use of object orientation concepts in the JSP to open aconnection and save the record into a database.

I dont want the conventional way of harcoding like registering DriverManager... Creating Statement opject and OPening Connection...... and with Statement object to call executeUpdate() method...

These all should be encapsulated in class and methods so that my JSP looks simple......


Java Gurus, How do i get going.........
 
Adeel Ansari
Ranch Hand
Posts: 2874
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
- make a utility class to get db connection or use some third party connection pooling API

- make DAO to access db

- prevent your servlet/jsp from jdbc code

the flow would be somewhat like this,

jsp ---> servlet ----> handler -----> DAO, and the other way around
 
Kalyana Sundaram
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for Ur reply Adeel,

SO the purpose of utility class is to open and close the connection.
i will have some method called openConnection() and closeConnection() with which i can open and close database connections.

Does DAO mean Data Access Objct, what is that about.

How do i relate all the flow...
and why do the servlet comes into picture and What does that handler refer too.
I am a newbie... I found the reply to be too genric ....

i want the structure of methods in utility class and DAO class....

Can u name any site where i can find exaples on such Architecture...

I appreciate ur interests envinced on this topic....
 
Adeel Ansari
Ranch Hand
Posts: 2874
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
jsp ---> servlet ----> handler -----> DAO

you can take it as,

view ---> controller ----> modal ----> DAO

- as you said about the utility class sounds fine.
- servlet as a controller. for entertaining requests.
- DAO would have some methods like, addEmployee(), getEmployeeById(), updateEmployee(). we write our queries here.
- and handler is nothing but your business logic. it will work as a model.

you can have like this as well. it is simpler having no business logic or dealing db access and business logic together.

jsp ---> servlet -----> DAO

you can take it as,

view ---> controller ----> modal
 
Kalyana Sundaram
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do need ur Support.....

1) This is a Html File. There are two form fields. If i click the submit button, it will call a JSP and the jsp will display the vslues in the form fields using Java beans.

---------------------------------------------------------------------

HTML file.

---------------------------------------------------------------------

<html>

<body>

<center><h1>Add This Facility to Facility Table</h1>

<br> <br> <br> <br>

<form name="addFacility" method=post action="AddFacility.jsp">

<table>

<tr><td>Facility ID :<td><input type=text name=f_id>

<tr><td>Facility Name :<td><input type=text name=f_name>

<tr><td><center><input type=submit><td><center><input type=reset>

</table>

</form>

</body>

</html>

---------------------------------------------------------------------

Java Bean Source

---------------------------------------------------------------------

package myPack;

public class AddFacilityBean {

int f_id;

String f_name;

public void setF_id(int f_id) { this.f_id = f_id; }

public void setF_name(String f_name) { this.f_name = f_name; }

public int getF_id() { return f_id; }

public String getF_name() { return f_name; }

}

---------------------------------------------------------------------

JSP file

---------------------------------------------------------------------

<h1> Facility Added </h1>

<jsp:useBean id="addFac" class="myPack.AddFacilityBean" scope="request"/>

<jsp:setProperty name="addFac" property="*" />

<jsp:getProperty name="addFac" property="f_id" />

<br>

<jsp:getProperty name="addFac" property="f_name" />

---------------------------------------------------------------------

All these code is executing fine. I dont have any doubt in this.

My question is I want to save the field values retrieved from the HTML form into the data base using the java bean and a helper class. How can i do that. Just give the structure of the helper class and where do i make changes in helper class and jsp file.

As u have mentioned that i have to make use of a controller like a servlet to dispatch the control to a model. I dont feel like making use of servlets.

Do I write further to explain what really I am looking for...

Expecting ur reply...........
Thanx a lot in Advance for your interest with this topic...

Kalyan........
 
Adeel Ansari
Ranch Hand
Posts: 2874
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you should forget about the servlet and DAO. do all in JSP. But then your JSP wouldn't look simple.

get the connection in jsp from your utility class and query the DB. or you may invoke some method of your handler class that is responsible to take the connection and do the transaction.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!