您好,登錄后才能下訂單哦!
這期內容當中小編將會給大家帶來有關使用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 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>
上述就是小編為大家分享的使用Vue怎么調用后端java接口了,如果剛好有類似的疑惑,不妨參照上述分析進行理解。如果想知道更多相關知識,歡迎關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。