Vue調(diào)用后端java接口的實例代碼
更新時間:2019年10月28日 17:18:56 作者:2heal
今天小編就為大家分享一篇Vue調(diào)用后端java接口的實例代碼,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
前段時間 做了個學(xué)校的春萌項目,其中用到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 {
}
}
前端調(diào)用:
<!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>
以上這篇Vue調(diào)用后端java接口的實例代碼就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
您可能感興趣的文章:
- java ZXing生成二維碼及條碼實例分享
- JAVA解析XML字符串簡單方法代碼案例
- Java簡易登錄注冊功能實現(xiàn)代碼解析
- Vue+Java 通過websocket實現(xiàn)服務(wù)器與客戶端雙向通信操作
- Vue.Js及Java實現(xiàn)文件分片上傳代碼實例
- Java基于jeeplus vue實現(xiàn)簡單工作流過程圖解
- vue+ java 實現(xiàn)多級菜單遞歸效果
- Vue請求java服務(wù)端并返回數(shù)據(jù)代碼實例
- 一個Java程序猿眼中的前后端分離以及Vue.js入門(推薦)
- vue+element+Java實現(xiàn)批量刪除功能
- java+vue實現(xiàn)添加單選題、多選題到題庫功能
- vue+Java后端進(jìn)行調(diào)試時解決跨域問題的方式
- Vue+Java+Base64實現(xiàn)條碼解析的示例
相關(guān)文章
UniApp中實現(xiàn)類似錨點(diǎn)定位滾動效果
一個uniapp小程序的項目,我們需要實現(xiàn)一個非常實用的功能——類似于錨點(diǎn)定位的交互效果,即在首頁中有多個tab(分類標(biāo)簽),每個tab對應(yīng)著不同的模塊,當(dāng)用戶點(diǎn)擊某個分類的tab時,需要流暢地滾動到對應(yīng)的內(nèi)容位置,提供更好的用戶體驗,2023-10-10
Vue使用electron轉(zhuǎn)換項目成桌面應(yīng)用方法介紹
Electron也可以快速地將你的網(wǎng)站打包成一個原生應(yīng)用發(fā)布,下面這篇文章主要給大家介紹了關(guān)于Vue和React中快速使用Electron的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-11-11
Antd-vue Table組件添加Click事件,實現(xiàn)點(diǎn)擊某行數(shù)據(jù)教程
這篇文章主要介紹了Antd-vue Table組件添加Click事件,實現(xiàn)點(diǎn)擊某行數(shù)據(jù)教程,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-11-11
vue自定義指令實現(xiàn)元素滑動移動端適配及邊界處理
這篇文章主要為大家介紹了vue自定義指令實現(xiàn)元素滑動移動端適配及邊界處理詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-09-09

