servlet/jsp/jdbc best approach

E

Email2us

Hello guys

Which is the best approach when developing servlet/jsp/jdbc? I will take an
example:

I have index-page, page2 and page 3.

When the user writes www.mydomain.com then the page index.jsp checks if the
user is coming from the servlet, like this:

<%
if(request.getAttribute("getPageTitle")==null){
%>
<jsp:forward page="/Index" />
<%
}
else{
%>
<%@ include file="/include/header.jsp" %>
content for index.jsp
<%@ include file="/include/footer.jsp" %>
<%
}
%>

On the other hand, the Index class has this init()

public void init(ServletConfig config) throws ServletException {
super.init(config);
con = DatabaseConf.getConnection();
}

DatabaseConf is a class that handles all the operations related to the
database.

Index.class has also the doGet-method like this:

request.setAttribute ("getPageTitle", "AnyTilte");
getServletConfig().getServletContext().getRequestDispatcher("/index.jsp").forward(request,
response);

Does it looks ok so long?

Now, I have other pages, page2 and page3 (in fact I have mych more than just
these, but this is only an example)

pages.jsp works as index.jsp. It makes a check
<%
if(request.getAttribute("getPageTitle")==null){
%>

if not satisfied than it redirect to

<jsp:forward page="/Page2" />

My concern, however, is about databasemanagement. Even the Page.class has an
init-method that looks exactly like the one in Index.class, e.g.

public void init(ServletConfig config) throws ServletException {
super.init(config);
con = DatabaseConf.getConnection();
}

In conclusion, Im starting jdbc-connection for every class-file that
initiates. Is this a good approach? What I would like is a solution to
initiate the database only one time, when the server starts. Not everytime,
the first time a specially class-file will be called. I know, this will
happens only one time for a file for the server's lifetime. But Im afraid
this is not a good way to work.

Regards

email2us
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,189
Members
46,734
Latest member
manin

Latest Threads

Top