java+Servlet+html代码

依赖

<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>org.example</groupId>
  <artifactId>Json</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>war</packaging>

  <name>Json Maven Webapp</name>
  <!-- FIXME change it to the project's website -->
  <url>http://www.example.com</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.7</maven.compiler.source>
    <maven.compiler.target>1.7</maven.compiler.target>
  </properties>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>com.google.code.gson</groupId>
      <artifactId>gson</artifactId>
      <version>2.6.2</version>
    </dependency>
    <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>javax.servlet-api</artifactId>
      <version>3.0.1</version>
      <scope>provided</scope>
    </dependency>
    <dependency>
      <groupId>javax.servlet.jsp</groupId>
      <artifactId>jsp-api</artifactId>
      <version>2.2</version>
      <scope>provided</scope>
    </dependency>
  </dependencies>

  <build>
    <finalName>Json</finalName>
    <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
      <plugins>
        <plugin>
          <artifactId>maven-clean-plugin</artifactId>
          <version>3.1.0</version>
        </plugin>
        <!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging -->
        <plugin>
          <artifactId>maven-resources-plugin</artifactId>
          <version>3.0.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-compiler-plugin</artifactId>
          <version>3.8.0</version>
        </plugin>
        <plugin>
          <artifactId>maven-surefire-plugin</artifactId>
          <version>2.22.1</version>
        </plugin>
        <plugin>
          <artifactId>maven-war-plugin</artifactId>
          <version>3.2.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-install-plugin</artifactId>
          <version>2.5.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-deploy-plugin</artifactId>
          <version>2.8.2</version>
        </plugin>
      </plugins>
    </pluginManagement>
  </build>
</project>

web.xml代码块

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>JsonTest</display-name>
  <welcome-file-list>
    <welcome-file>WEB-INF/index.jsp</welcome-file>
  </welcome-file-list>
  <servlet>
    <servlet-name>NewsServlet</servlet-name>
    <servlet-class>NewsServlet</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>NewsServlet</servlet-name>
    <url-pattern>/getJson</url-pattern>
  </servlet-mapping>

</web-app>

ajax代码块

<!DOCTYPE html>

<!-- <meta http-equiv="Access-Control-Allow-Origin" content="*" /> -->

<html>

<head>

    <meta charset="utf-8">

    <title>菜鸟教程(runoob.com)</title>

    <script src="js/jquery-3.3.1.min.js">

    </script>

    <script>

    $(document).ready(function(){

    });

    $(function(){

       // $("button").click(function(){

        $.ajax({

            url: 'http://localhost:8080/getJson',

             type: "get",

          crossDomain: true,

          xhrFields: {

            withCredentials: true

          },

          dataType: "json",

          data: {

            "yngrbsh":"31010920090306250337209",

            "pglsh":"157a0a07-73e9-450a-b835-3f396b3b7acb",

            "jbdms":"JB_GXY;JB_NCZ;JB_TNB_YH"

          },

      success: function (data) {

            let h33 = document.getElementById("h33")

            var html = "";

            console.log(data[0].content)  

            h33.innerText = data[0].content

            for (var i = 0; i < data.length; i++) {

                html+="<tr>"+

                    "<td>"+data[i].nId+"</td>"+

                    "<td>"+data[i].title+"</td>"+

                    "<td>"+data[i].content+"</td>"+

                    "<td>"+data[i].date+"</td>"+

                    "<td>"+data[i].url+"</td>"

                    +"</tr>"

            }

            $("#tbody_tr").html = html

            $("#tbody_tr").append(html);

      }

    });

            // });

    })

    </script>

</head>

<body>

    <table>

        <thead>

            <tr>

                <td>路径类型名称</td>

                <td>路径ID</td>

                <td>路径名称</td>

                <td>请求数量</td>

            </tr>

        </thead>

        <tbody id="tbody_tr">



        </tbody>

       

    </table>



<button>发送一个 HTTP GET 请求并获取返回结果</button>

<h3 id="h33"></h3>

</body>

</html>

JDBC代码块

package com.JDBC;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

public class JDBC {
    private static final String name = "com.mysql.jdbc.Driver";
    private static final String url = "jdbc:mysql://127.0.0.1:3306/zuoye";
    private static final String root = "root";
    private static final String pass = "*******";

//加载驱动
    static{
        try {
            Class.forName(name);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
//    链接驱动
    public static Connection getConn(){
        Connection cn = null;
        try {
            cn= DriverManager.getConnection(url,root,pass);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return cn;
    }
//关闭链接
    public static void CloseCon(Connection con , PreparedStatement pstm , ResultSet rs){
        try {
            if(con!=null){
                con.close();
            }
            if(pstm!=null){
                pstm.close();
            }
            if(rs!=null){
                rs.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    public static void main(String[] args) {
        System.out.println(getConn());
    }

}

dao查询所有模块

package com.Dao;

import com.JDBC.JDBC;
import com.Model.Workflow_Base;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

public class Workflow_BaseDao {
    Connection con = null;
    PreparedStatement pstm = null;
    ResultSet rs = null;
    String sql = "";
//查询所有
    public List<Workflow_Base> selectAll(){
        List<Workflow_Base> arr = new ArrayList<Workflow_Base>();
        con = JDBC.getConn();
        sql = "select id, workflowtype, workflowname, formid from WORKFLOW_BASE";
        try {
            pstm = con.prepareStatement(sql);
            rs = pstm.executeQuery();
            while (rs.next()){
                int id = rs.getInt("id");
                String workflowtype = rs.getString("workflowtype");
                String workflowname = rs.getString("workflowname");
                String formid = rs.getString("formid");
                Workflow_Base wb = new Workflow_Base(id,workflowtype,workflowname,formid);
                arr.add(wb);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            JDBC.CloseCon(con,pstm,rs);
        }
        return arr;
    }

    public static void main(String[] args) {
        Workflow_BaseDao wdb = new Workflow_BaseDao();

        List<Workflow_Base> workflow_bases = wdb.selectAll();
        for (Workflow_Base w :workflow_bases) {
            System.out.println(w.getWorkflowname());
        }

    }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值