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方法)相关推荐
- div 夸站点加载html,使用jQuery加载html页面到指定的div实现方法
一.jQuery加载一个html页面到指定的div里 把a.html里面的某一部份的内容加载到b.html的一个div里. 比如:加载a.html里面的 用jquery ajax 可以实现 假设 a. ...
- layui父页面调用子页面的渲染_layUI ajax加载html页面后重新渲染的方法
相关问题同: 1.layUI使用jquery.load加载界面时,如何让layui渲染页面? 2.layUI ajax加载html页面后重新渲染 layUI ajax加载html页面后渲染,关键在将h ...
- Java虚拟机中 类的加载过程
Java中 类的加载过程 例如下面的一段简单的代码 public class HelloWorld {public static void main(String[] args) {System.ou ...
- 鸿蒙系统中的 WebView 加载H5页面出现net::ERR_CLEARTEXT_NOT_PERMITTED的解决方法
目前在学习鸿蒙系统开发相关的应用,在加载H5页面的时候,在页面上显示出现这也错误提示 net::ERR_CLEARTEXT_NOT_PERMITTED,百度了一番,显示的结果都是在安卓系统上的解决办法 ...
- Java 类中各成分加载顺序和内存中的存放位置
一.什么时候会加载类? 使用到类中的内容时加载:有三种情况 1.创建对象:new StaticCode(); 2.使用类中的静态成员:StaticCode.num=9; StaticCode.show ...
- java类使用其他类的变量_如何将java类中的变量加载到其他类
我之前已经在类之间加载了变量,但现在无法使它工作 . 首先,我有我的代码,我尝试加载它 . 包XMLTest; public class ModifyTTXML { public static voi ...
- flutter 中使用 WebView加载H5页面异常net:ERR_CLEARTEXT_NOT_PERMITTED
最近有个flutter项目中根据搜索结果跳转到相应的H5页面发现老是报错,曾现在闲暇拉出来解决哈 先来看一个搜索功能的测试 已进入详情页面就提示错误,尴尬了. 只有去检测代码了撒 Search.dar ...
- html 进入页面延迟加载数据,跳转至预加载的页面后数据显示延迟问题
请问各位大神: 从A页面跳转到预加载好的B页面,B页面要用ajax从服务器中取出数据并显示,现在跳过去后会先显示页面,但是数据会延迟1秒左右才会显示.这该如何解决,或者有什么办法可以让B页面的数据完全 ...
- JAVA加载 编译 运行,在Java 7中编译的加载/运行类6
I am using some client jars in my component the client jars are compiled and run with java 7.I am no ...
最新文章
- mnist手写数字数据集_mnist手写数据集(1. 加载与可视化)
- rpm的mysql安装_MySQL 5.7.22 rpm 安装方式
- Sobel两种算子形式
- 一起教育科技登陆纳斯达克:首日股价上涨0.67%
- lucene 分词相关的类
- CSS中超链接样式的书写顺序
- 四种方法求Capacitated Facility Location Problem问题
- Ubuntu安装GCC 3.4
- 3dmax2020渲染器下载3dmax2020渲染器VRay4.2下载安装教程
- eclipse优化记录
- 微信小程序上传图片到阿里云
- 文件后缀对应文件类型表
- 异数OS-星星之火(二)--远程实验室注册开放
- K9G8G08U0A升级到K9GAG08U0D烧录NK要注意的地方
- MySQL中按天、自然周、月、季度、年份统计
- 装甲逆袭-NPC移动处理
- 深入CSS vertical-align属性
- 区块链能否遏制公共部门的腐败行为?
- Flutter:状态管理(5) --BLoC
- 2020程序员大厂面试流程,面试游刃有余!