登录

世界上最可怕的事情就是查了一天的BUG还没查明白,我甚至一度以为是我都eclipse出了问题,直到转到idea看到相同的问题之后我才发现是代码的问题

可喜可贺,终于解决了登录问题,于是马上分享出来

依旧是在eclipse上进行的操作

结构如下:此篇还没有涉及到注册

 

 依旧是熟悉的结构,就不再赘述了,有需要可以去翻看我之前的博客

登录界面 land.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import ="java.sql.*"%>
<%@ page import ="DBUtil.DBUtil"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>登录</title>
</head>
<body>
<center>
<form action="landservlet" method="post">
<p style="height: 20px; border: 1px solid red;text-align: center;">登录</p>
<p>
请输入账户:<input type="text" name="id" maxlength="10">
<p/>
<p>
请输入密码:<input type="text" name="psd" maxlength="10">
<p/>
<input type="hidden" name="method" value="land"/>
<input type="submit" name="submit" value="登录">
</form>
</center>
</body>
</html>

录入信息之后跳转到servlet界面

package BeanServlet;

import java.io.IOException;
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 Dao.landDao;

/**
* Servlet implementation class landservlet
*/
@WebServlet("/landservlet")
public class landservlet extends HttpServlet {
private static final long serialVersionUID = 1L;

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
//response.getWriter().append("Served at: ").append(request.getContextPath());
//防止乱码
response.setCharacterEncoding("UTF-8");
response.setContentType("text/html;charset=UTF-8");
request.setCharacterEncoding("UTF-8");

//判断前端传来的标记,以此执行相对应的操作
String method=request.getParameter("method");
System.out.print(method);
if("land".equals(method)) {
System.out.print("开始验证");
String id=request.getParameter("id");
String password=request.getParameter("psd");
int flag = landDao.land(id,password);
if(flag != 0) {
System.out.print("登录成功");
request.getRequestDispatcher("index.jsp").forward(request,response);
} else {
System.out.print("登录失败");
request.getRequestDispatcher("land.jsp").forward(request,response);
}
}
}

/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}

}

注意这里让eclipse自动创建servlet

之后跳转到到层进项查询

package Dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import DBUtil.DBUtil;

public class landDao {

public static int land(String id, String password) {
// TODO Auto-generated method stub
Connection connection=null;
PreparedStatement preparedStatement=null;
connection= DBUtil.getConnection();
int flag=0;
System.out.print("即将开始查找");
String sql="select * from land where id=? and password=?";
try{
System.out.print("开始查找");
preparedStatement=connection.prepareStatement(sql);
preparedStatement.setString(1,id);
preparedStatement.setString(2,password);
ResultSet resultSet = preparedStatement.executeQuery();
while (resultSet.next()){
String id2=resultSet.getString(1);
String password2=resultSet.getString(2);
String found = id2 + password2;
if(found != null ||found.length() != 0) {
flag = 1;
}
}
System.out.println("flag=" + flag);
if(flag != 0) {
System.out.print("用户信息存在");
} else {
System.out.print("用户信息不存在");
}
preparedStatement.close();
connection.close();

}catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
DBUtil.close(preparedStatement,connection);
}
return flag;
}

}

这一部分是我之前程序无法正常运行的地方

 

 我错误的使用了SQL语句导致程序无法运行,这个失误让我卡了将近一天QWQ

 

主要部分的代码基本就是这些,按照我的思路,登录成功之后会进入主界面,而登录失败则会返回注册界面

posted @   椰子灰  阅读(46)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
点击右上角即可分享
微信分享提示