Link to home
Start Free TrialLog in
Avatar of dr0zaxx
dr0zaxx

asked on

How to convert these java codes (for a feedback) to javabean?

Can anyone please help me to convert these java codes (for feedback) to javabean (using the MVC Model-View-Controller pattern design)?

<%

//instantiate variables
Connection con = null;
Statement stmt = null;
Statement stmt2 = null;
ResultSet rs = null;
String queryString;
int newInBoxMsg = 0;
int newSentMsg = 0;
int newSavedMsg = 0;
int newTrashCanMsg = 0;
String currentUserID = 1+"";
String adminID = 1+""; //change this ID to your adminID in the db

try
{
//Load the JDBC driver
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver").newInstance();

//Get the connection getConnection("access driver", "userID", "password")
con = DriverManager.getConnection("jdbc:odbc:FREN_DB","","");
stmt = con.createStatement();

//sql statements: create, update, query
Calendar cl = Calendar.getInstance();
%>

<%
if(request.getMethod()=="POST"){

int y = stmt.executeUpdate("INSERT into Feedback(`whom`, `msg`, `date`)"
+" values ('"+currentUserID+"', '"+request.getParameter("date")+"', '"
+request.getParameter("msg")+"')");

int x = stmt.executeUpdate("INSERT into MailBox(`whom`, `who`, `mailheader`, `mailbody`, `date`)"
+" values ('"+adminID+"', '"+currentUserID+"', 'Feedback', 'We have received your feedback and we will respond to you as soon as possible', '"+request.getParameter("date")+"')");

out.println("Feedback Sent!");
}
else {
%>

<form name="compose" method="POST" action="Feedback.jsp">
<input name="date" type="hidden" value="<% out.println(cl.get(cl.DAY_OF_MONTH)+"/"+cl.get(cl.MONTH)+"/"+cl.get(cl.YEAR)); %>" maxlength="20">
<p>Your Feedback</p>
<p>
<textarea name="msg" cols="50" rows="10"></textarea>
</p>
<p>
<input type="submit" name="Submit" value="Submit">
</p>
</form>

<%

}

}catch(Exception e) {
System.out.println(e);
}


%>
Avatar of bloodredsun
bloodredsun
Flag of Australia image

Rather than going to struts, the simplest MVC method for this would be to use a servlet to get the data from the db (controller-ish), populate a javabean (Model), put the javabean in the request, and then use RequestDispatcher to forward to a jsp (View).
Avatar of ashok3sep
ashok3sep

Please read the following link to understand what is behind the MVC architecture
and you would better have a clear Idea.

http://www.oracle.com/technology/sample_code/tech/java/j2ee/jintdemo/tutorials/Struts.html

regards,

Freedom.
I agree with bloodredsun...

Using a framework would be overkill if this is the only thing you have to convert...  But just putting code in a class, then calling that class from the JSP does not make it MVC...it just makes it a bit neater...

You will need to follow bloodredsun's advice to get "true MVC"

The main issue I can see with your code is that someone could potentially perform an SQL Injecttion attack on your database (as you aren't using PreparedStatements), and you should be using a connection pool to get your connection (as this will speed things up)

Tim
Avatar of dr0zaxx

ASKER

Hmm.. can anyone please direct convert the codes for me here?
I would also recommend using a DataSource and PreparedStatements for your database interactions.
Ha! Sorry Tim didn't refresh before the PS comment.
ASKER CERTIFIED SOLUTION
Avatar of bloodredsun
bloodredsun
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
It would actually be a little better to replace the date value created in the scriplet in feedback_form.jsp with data from a bean that is created in the servlet and passed to the jsp in the request. In this case, the bean could repersent part of your model and also function as a DTO (Data Transfer Object) which is always good coding practise.

e.g.
in servlet
------------
Calendar cl = Calendar.getInstance();
FeedbackBean = myFeedbackBean = new FeedbackBean() ;
myFeedbackBean.setDate( cl.DAY_OF_MONTH +  cl.MONTH + cl.YEAR ) ;
//set in request
request.setAttribute( "feedbackBean" , myFeedbackBean ) ;

in jsp, using JSTL to output the bean's value
---------
<input name="date" type="hidden" value="<c:out value='${requestScope.feedbackBean.date}' />" maxlength="20">

This creates the possiblity of passing the bean throughout your application so creating a model of your data.

And you'll need to do a null check before the servlet line
>>if (request.getParameter("action").equals("feedback")){

as otherwise it will throw a nullPointerException.