AJAX with JSP and Servlet (CODE), ajaxservlet
You are welcome to reprint it in any form, but be sure to indicate the source.
This chapter is about FQ for YouTube (click to learn from the video)
For more information about server configurations, see my other articles. Add comments
Structure
1 <body> 2 <fieldset> 3 <ledend>Demo 1</ledend> 4 <form> 5 Name <input type="text" id="fullname"> 6 <input type="button" value="Hello" id="bttHello"> 7 <br> 8 <span id="result1"></span> 9 </form>10 </fieldset>11 12 <fieldset>13 <ledend>Demo 2</ledend>14 <form>15 Number 1<input type="text" id="number1"><br>16 Number 2<input type="text" id="number2"><br>17 Result<span id="result2"></span><br>18 <input type="button" value="Sum" id="bttSum">19 20 21 </form>22 </fieldset>23 24 </body>
Body section
1 Ajax Section
1 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 2 // TODO Auto-generated method stub 3 response.setContentType("text/plain"); 4 PrintWriter out = response.getWriter(); 5 String action = request.getParameter("action"); 6 if(action.equals("demo1")){ 7 String fullname = request.getParameter("fullname"); 8 out.print("Hello"+fullname); 9 }10 else if(action.equals("demo2")){11 int number1 = Integer.parseInt(request.getParameter("number1"));12 int number2 = Integer.parseInt(request.getParameter("number2"));13 out.println(number1+number2);14 }15 16 }
Servlet Section