Java web中使用JQuery加载某页面后,自动调用Servlet(GET方法,POST方法)
生活随笔
收集整理的這篇文章主要介紹了
Java web中使用JQuery加载某页面后,自动调用Servlet(GET方法,POST方法)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
目錄
?
背景
演示
源碼
?
背景
比如需要在頁面加載后,干某些事情,就需要自動調用Servlet去調用Java代碼,以達到某種功能。
?
演示
web網頁搭建好后,進入這個web頁面,自動調用Servlet
Servlet輸出Hello:
?
源碼
java項目結構如下:
這里要注意:使用了jquery.js,要把這個導入進去!
源碼如下:
SomeInof.java
package my;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;public class SomeInfo extends HttpServlet {/*** Constructor of the object.*/public SomeInfo() {super();}/*** Destruction of the servlet. <br>*/public void destroy() {super.destroy(); // Just puts "destroy" string in log// Put your code here}/*** The doGet method of the servlet. <br>** 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/html"); // PrintWriter out = response.getWriter(); // out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">"); // out.println("<HTML>"); // out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>"); // out.println(" <BODY>"); // out.print(" This is "); // out.print(this.getClass()); // out.println(", using the GET method"); // out.println(" </BODY>"); // out.println("</HTML>"); // out.flush(); // out.close();System.out.println("Hello");}/*** The doPost method of the servlet. <br>** 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/html");PrintWriter out = response.getWriter();out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");out.println("<HTML>");out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>");out.println(" <BODY>");out.print(" This is ");out.print(this.getClass());out.println(", using the POST method");out.println(" </BODY>");out.println("</HTML>");out.flush();out.close();}/*** Initialization of the servlet. <br>** @throws ServletException if an error occurs*/public void init() throws ServletException {// Put your code here}}index.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html><head><base href="<%=basePath%>"><title>My JSP 'index.jsp' starting page</title><meta http-equiv="pragma" content="no-cache"><meta http-equiv="cache-control" content="no-cache"><meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"><meta http-equiv="description" content="This is my page"><!--<link rel="stylesheet" type="text/css" href="styles.css">--><script src="jquery/jquery.js"></script><script type = "text/javascript">function autoLoad(){jQuery.ajax({method: "GET",url: "servlet/SomeInfo"});}</script></head><body onload = "autoLoad()"></body> </html>關鍵就是在<body onload = "autoLoad()">這個邏輯
總結
以上是生活随笔為你收集整理的Java web中使用JQuery加载某页面后,自动调用Servlet(GET方法,POST方法)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: HTTP之Cache-Control基本
- 下一篇: Java笔记-多线程相关