-3

index.html

 <form action="table2.jsp">
      Name:<input type="text" name="j"><br>
      Address:<input type="text" name="k"><br>
      phone:<input type="text" name="h"><br>
      <input type="Submit" value="SUBMIT"><br>
      <input type="button" value="SAVE" onclick="table1.jsp">
 </form>

table1.jsp

<% String a,b,c;
        int d=0,e=0,f=0;
        String l[]=new String[d];
        String m[]=new String[e];
        String n[]=new String[f];
        a=String.valueOf(request.getParameter("j"));
        b=String.valueOf(request.getParameter("k"));
        c=String.valueOf(request.getParameter("h"));
        l[0]=String.valueOf(a);
        m[0]=String.valueOf(b);
        n[0]=String.valueOf(c);
        out.print("SAVED");
        d++;e++;f++;
        %>

on clicking "SAVE" button it should run the table1.jsp and the values from the textfield in the index.html should get inside the array created in table1.jsp and increment the value of d,e,f every time "SAVE" button is clicked.Help me find out what mistake am I doing?

Kohei TAMURA
  • 4,970
  • 7
  • 25
  • 49
  • Anyone help to create a function in jsp, that i can call using an onclick event in the index.html...for the jsp function please refer table1.jsp – Deepraaj Ghosh Jun 16 '17 at 15:45

2 Answers2

1

1) onclick expects JavaScript code, so you should ideally put a call to a JS function handling the event there; in this function, you can use AJAX call to call the table1.jsp code; anyway

2) such code does not belong to JSP, it should be in a servlet;

3) moreover, remember (or learn) that all local variables in JSP/Servlets, such as your d, e and f, 'live' only within a request, i.e. they would be reset to 0 for each "SAVE" call; if you want them to survive, you have to store them into session scope (or maybe application scope, depending on what should they represent);

4) last but not least: using parameter and variable names such as a, b, c, d, etc. is extremely bad practice and I would scold you if you committed such code into a project I am working on;

Jozef Chocholacek
  • 2,874
  • 2
  • 20
  • 25
  • "I would scold you if you committed such code into a project I am working on"--what?who the heck are you? – Deepraaj Ghosh Jun 15 '17 at 14:50
  • 1
    _who the heck are you?_ A guy who tried to help you. But if we worked on the same project, I'd be your senior colleague, if not your superior. Anyway, it was not meant to offend you, just to emphasize how bad this practice is - one should use meaningful variable/method names even in smallest school projects, just to get used to it. – Jozef Chocholacek Jun 15 '17 at 14:59
  • ok then help me to create a jsp function in index.html that would insert the values of the textfields in arrays using onclick and lets forget about increment of d,e,f. – Deepraaj Ghosh Jun 15 '17 at 15:46
  • alright Mr hypocrite enough of your hypocrisy and here after don't speak nonsense unless you are an expert. – Deepraaj Ghosh Jun 16 '17 at 15:41
  • 1
    @DeepraajGhosh: if you're goal with these comments is to convince no one to help you out of fear of getting backlash from you, then continue because it's working quite well. – Hovercraft Full Of Eels Jun 16 '17 at 16:26
  • see my goal is to get a solution not to continue a fruitless argument.so i am waiting for an answer from someone who knows and not from a self proclaimed senior who is all talks and no trousers @Jozef Chocholacek. – Deepraaj Ghosh Jun 17 '17 at 05:36
1

my solution would first create a servlet let's say abc.java, then in the doGet method of the abc.java, using RequestDispatcher dispatch your JSP file!. Then in the button argument use the following syntax.

<button onclick="location.href = './abc'">take me to required jsp file</button>

remember abc is the servlet filename!... Hope this helps, since I'm not working on it currently, I haven't posted any code. If you want please comment and I will try in my spare time!.

hemanth
  • 62
  • 5