Wednesday, March 05, 2008

Repeatitive process in Servlet

Hishashiburi neh.
It’s been a long time before I post this solution.Actually I got a lot of problems and solutions but I didn’t have time to post it in my blog.So unlucky!
But right now I just steal some time to update and post this entry right after I got the solution. It’s been three days I faced this problem. It’s challenge my ability as programmer because as normal programmer, we will feel strong if our code works, is not, I feel that I am not good programmer at all,I should never be born as a programmer.
Problem: My servlet is keep repeating implement some code once you submit form from jsp page.I think it should not repeat if we don’t want to.

Example

public class PlacementServlet extends HttpServlet {
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
try{
String mode=request.getParameter("mode");
System.out.println("MODE ------------------------------>"+mode);
if(mode.equals("i can change my life ")){
//do something for example insert into databases
}
else {
System.out.println(Bye- bye)
}
//more codes
Placement. jsp sample code

<script>
function doSubmit(){
var frm=document.placement
frm.mode.value="i can change my life";
frm.action = "PlacementServlet";
frm.submit();}
</script>
<form name="placement">
<input name="btnsave" type="submit" class="button" id="btnsave" value="SAVE" onclick="javascript:doSubmit()">
</form>

So when I clicked SAVE button, the output in my NetBeans produce :
MODE ------------------------------> i can change my life
MODE ------------------------------> i can change my life

Which if I do code that inserting to database; it will insert two times and prompt error violent to primary key. I already get sick with the problem and modify almost entire coding until I got to see

<input name="btnsave" type="submit" class="button" id="btnsave" value="SAVE" onclick="javascript:doSubmit()">
I change it to type="button" , and now the stupid repeatiting thing stop.I thinks it happen because it implement two command, first submit from javascript (doSubmit()) and then submit from the button. But that’s only what I think.
So case is closed, right know I need to rewrite back the code that I modified due to my stupidiness.