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

