基本的JSP中调用Ajax与Servlet进行数据交互

首先创建jsp页面中  然后在页面中写入js代码!
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8" contentType="text/html; charset=UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>



  
    
    
    My JSP 'index.jsp' starting page
	
	
	    
	
	
	
	
    
  
  
    This is my JSP page. 


result : 写一个Servlet类 package com.hal.servlet; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.xml.ws.Dispatch; public class testServlet extends HttpServlet {     /**      *      */     private static final long serialVersionUID = 1L;     /**      * Constructor of the object.      */     public testServlet() {         super();     }     /**      * Destruction of the servlet.
     */     public void destroy() {         super.destroy(); // Just puts "destroy" string in log         // Put your code here     }     /**      * The doGet method of the servlet.
     *      * This method is called when a form has its tag value method equals to get.      *      * @param request the request send by the client to the server      * @param response the response send by the server to the client      * @throws ServletException if an error occurred      * @throws IOException if an error occurred      */     public void doGet(HttpServletRequest request, HttpServletResponse response)             throws ServletException, IOException {         response.setContentType("text");         String   username = request.getParameter("username");         System.out.println(username);         PrintWriter out = response.getWriter();         out.write(username);         request.getRequestDispatcher("index.jsp").forward(request, response);         out.flush();         out.close();     }     /**      * The doPost method of the servlet.
     *      * This method is called when a form has its tag value method equals to post.      *      * @param request the request send by the client to the server      * @param response the response send by the server to the client      * @throws ServletException if an error occurred      * @throws IOException if an error occurred      */     public void doPost(HttpServletRequest request, HttpServletResponse response)             throws ServletException, IOException {         response.setContentType("text");         PrintWriter out = response.getWriter();         out.flush();         out.close();     }     /**      * Initialization of the servlet.
     *      * @throws ServletException if an error occurs      */     public void init() throws ServletException {         // Put your code here     } } 配置web.xml文件   Ajaxex       This is the description of my J2EE component     This is the display name of my J2EE component     AjaxexServlet     com.hal.servlet.AjaxexServlet         This is the description of my J2EE component     This is the display name of my J2EE component     testServlet     com.hal.servlet.testServlet         AjaxexServlet     /AjaxexServlet         testServlet     /testServlet         index.html     index.htm     index.jsp     default.html     default.htm     default.jsp  

你可能感兴趣的:(JavaWeb)