Vue调用后端java接口的实例代码

作者:2heal 时间:2024-05-09 10:51:02 

前段时间 做了个学校的春萌项目,其中用到vue连接后端java接口。

先上后端接口代码:


package controller;

import net.sf.json.JSONObject;
import util.DBUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

@WebServlet(name = "login",urlPatterns = "/login")
public class login extends HttpServlet {
 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
   HttpSession session = request.getSession(true);
   String username = request.getParameter("username");
   String password = request.getParameter("password");
   DBUtil dbUtil = new DBUtil();
   Connection connection = dbUtil.getConnection();
   PreparedStatement preparedStatement;
   ResultSet rs;
   String psw="";
   String sql = "select password from `user` where username=?";
   try {
     preparedStatement = connection.prepareStatement(sql);
     preparedStatement.setInt(1,Integer.parseInt(username));
     rs = preparedStatement.executeQuery();
     while (rs.next()){
       psw = rs.getString("password");
     }
   }
   catch (Exception e){
     e.printStackTrace();
   }
   if (password.equals(psw)){
     session.setAttribute("username",username);
     response.getWriter().print("true");
   }
   else
   response.getWriter().print("false");
 }

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

}
}

前端调用:


<!DOCTYPE html>
<html lang="en">
<head>
 <meta charset="UTF-8">
 <title>Login</title>
 <script src="node_modules/vue/dist/vue.js"></script>
 <!--axios基于promise-->
 <script src="node_modules/axios/dist/axios.js"></script>
 <script src="login.js"></script>
 <script src="https://cdn.bootcss.com/vue-resource/1.5.1/vue-resource.min.js"></script>
 <link rel="stylesheet" href="login.css" rel="external nofollow" >
</head>
<body>
<div class="login_interface" id="interface_height">
 <img src="ic_login_logo.png" alt="" class="login_logo">
 <span id="login_head">智慧图书管理平台</span>
 <div class="login_input">
   <img src="ic_login_number.png" alt="" class="login_number">
   <input type="text" value="请输入账号" id="input_number" v-model="username">
 </div>
 <div class="login_input" id="add_top">
   <img src="ic_login_password.png" alt="" class="login_number">
   <input type="text" value="请输入密码" id="input_password" v-model="password">
 </div>
 <button class="login_unselected" id="tick"></button>
 <span id="remember_password">记住密码</span>
 <button id="registered_now"><a href=""><span style=" rel="external nofollow" color: grey">立即注册</span></a></button>
 <button id="login" @click="login()">登录</button>
</div>
<script>
 new Vue({
   el:'#interface_height',
   data:{
     username:'',
     password:''
   },
   methods:{
     login:function () {
       this.$http.post('login',{username:this.username,password:this.password},{emulateJSON:true}).then(function(res){
         console.log(res.data);
         window.location.href = 'index.html';
       },function(res){
         console.log(res.status);
       });
     }
   },
   created:function(){
   }
 })
</script>
</body>
</html>

来源:https://blog.csdn.net/qq_39970183/article/details/85076609

标签:Vue,调用,java接口
0
投稿

猜你喜欢

  • asp之日期和时间函数示例

    2008-04-13 06:50:00
  • MySQL数据库远程访问权限设置方式

    2024-01-22 06:58:43
  • Django 删除upload_to文件的步骤

    2022-03-23 05:47:14
  • 什么是XML?

    2007-10-29 12:53:00
  • Django接收post前端返回的json格式数据代码实现

    2022-09-30 22:21:22
  • Python3内置模块pprint让打印比print更美观详解

    2022-02-04 01:55:28
  • Pygame 精准检测图像碰撞的问题

    2022-01-17 17:56:13
  • pandas的resample重采样的使用

    2023-04-07 10:33:29
  • Vue中的常用指令及用法总结

    2024-06-07 16:05:31
  • 关于 MySQL 嵌套子查询中无法关联主表字段问题的解决方法

    2024-01-18 23:05:28
  • 基于python实现的抓取腾讯视频所有电影的爬虫

    2023-02-20 22:01:39
  • JavaScript+canvas实现七色板效果实例

    2023-08-09 09:48:10
  • SQL Server 2005 FOR XML嵌套查询使用详解

    2009-01-06 11:20:00
  • python 定义类时,实现内部方法的互相调用

    2023-11-20 11:09:31
  • python进阶教程之词典、字典、dict

    2022-10-10 10:41:36
  • MSSQL分页存储过程完整示例(支持多表分页存储)

    2024-01-15 10:19:21
  • SQL Server数据表字段自定义自增数据格式的方法

    2024-01-13 03:23:45
  • Python 中如何实现参数化测试的方法示例

    2023-10-19 14:19:56
  • TOPI如何使TVM代码不那么样板化

    2022-02-02 00:22:07
  • pandas group分组与agg聚合的实例

    2023-01-04 14:22:28
  • asp之家 网络编程 m.aspxhome.com