承接网站建设文案,wordpress mx主题VIP,四川游戏seo整站优化,网站做推荐链接端口在后端进行POST请求后端接收前端数据时#xff0c;关于PrintWriter#xff1a;参考地址
注意传进来的对象为空说明未在数据库查到拥有该账号的用户#xff0c;所以需要告诉前端“账号密码不正确”#xff0c;如果不为空说明有该用户#xff0c;所以可以登录。 Overridep… 在后端进行POST请求后端接收前端数据时关于PrintWriter参考地址
注意传进来的对象为空说明未在数据库查到拥有该账号的用户所以需要告诉前端“账号密码不正确”如果不为空说明有该用户所以可以登录。 Overrideprotected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {String accountreq.getParameter(account);String passwordreq.getParameter(password);System.out.println(account);System.out.println(password);PrintWriter printWriterresp.getWriter();User user null;try {user dao.logindao(account,password);if(usernull) {printWriter.print(账号或密码不正确);}else {ObjectMapper objectMappernew ObjectMapper();String jsonobjectMapper.writeValueAsString(user);printWriter.print(json);}} catch (SQLException throwables) {throwables.printStackTrace();printWriter.print(服务器忙...请稍后重试);}}
最开始置对象为空如果没有查到该用户直接返回空。
import com.mysql.cj.jdbc.Driver;import java.sql.*;public class LoginDao {public User logindao(String account, String password) throws SQLException {String sqlselect * from user where account? and password?;Connection connectionDButils.getConnection();PreparedStatement preparedStatement connection.prepareStatement(sql);preparedStatement.setObject(1,account);preparedStatement.setObject(2,password);ResultSet resultSetpreparedStatement.executeQuery();User usernull;if(resultSet.next()){usernew User();String pswresultSet.getString(password);user.setAccount(account);user.setPassword(psw);}DButils.close(resultSet,preparedStatement,connection);return user;}
}前端代码需要注意我们使用的是原生的servlet前端传给后端的数据是JSON形式后端无法识别需要进一步对该数据进行处理才可以传给后端。
scriptexport default {data() {return {from: {account: ,password: }}},methods: {login() {this.$http.post(LoginServlet, jsonToString(this.from)).then(resp {if(resp.data账号或密码不正确){this.$message({message: 账号或密码不正确,type: warning});}else if(resp.data服务器忙...请稍后重试){this.$message({message: 系统忙,请稍后再试!,type: error});}else{this.$message({message: 登录成功,type: success});this.$router.push(/main);//在js中进行路由导航}})}},}//将json对象序列化为键值键值function jsonToString(jsonobj) {console.log(jsonobj)var str ;for (var s in jsonobj) {str s jsonobj[s] ;}return str.substring(0, str.length - 1);}
/script