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="https://www.jb51.net/node_modules/vue/dist/vue.js"></script> <!--axios基于promise--> <script src="https://www.jb51.net/node_modules/axios/dist/axios.js"></script> <script src="https://www.jb51.net/login.js"></script> <script src="https://cdn.bootcss.com/vue-resource/1.5.1/vue-resource.min.js"></script> <link href="https://www.jb51.net/login.css" > </head> <body> <div> <img src="https://www.jb51.net/ic_login_logo.png" alt=""> <span>智慧图书管理平台</span> <div> <img src="https://www.jb51.net/ic_login_number.png" alt=""> <input type="text" value="请输入账号" v-model="username"> </div> <div> <img src="https://www.jb51.net/ic_login_password.png" alt=""> <input type="text" value="请输入密码" v-model="password"> </div> <button></button> <span>记住密码</span> <button><a href=""><spanexternal nofollow" color: grey">立即注册</span></a></button> <button @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>

以上这篇Vue调用后端java接口的实例代码就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持脚本之家。

您可能感兴趣的文章:

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:http://www.heiqu.com/c5009ac5a0809d2ae171dc96ee96ba9e.html