package com.tnt.jsp;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import com.fasterxml.jackson.databind.ObjectMapper;
import entity.Users;
import Dao.UserDao;
/**
Servlet implementation class LoginServlet
*/
public class LoginJsonServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
/**
response)
*/
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
// response.setContentType("Textcml;charset=UTF-8");
String uname = request.getParameter("uname");
String upass = request.getParameter("upass");
// PrintWriter pw = response.getWriter();
Users condition = new Users();
condition.setUname(uname);
condition.setUpass(upass);
UserDao userDao = new UserDao();
Users user = userDao.login(condition)就是這裡一直無法調用????????
PrintWriter out = response.getWriter();
if (user != null) {//生成json字符串2
ObjectMapper mapper = new ObjectMapper();
String json = mapper.writeValueAsString(user);
out.write(json);
out.flush();
out.close();
}
// 得到session對象
// 如果傳false,當前session對象就返回session對象,如果不存在就返回新創建的對象
HttpSession session = request.getSession();
if (user != null) {
session.setAttribute("USER", user);
request.setAttribute("USERNAME", uname);
//請求轉發
request.getRequestDispatcher("/success.jsp ").forward(request,response);
// 重定向
response.sendRedirect("success.html");
request.getRequestDispatcher("/BloggersSearvlet").forward(request,
response);
System.out.println("login");
response.sendRedirect("BloggersSearvlet");
// pw.println(uname + "登陸成功!");
} else {
// pw.println(uname + "登錄失敗");
request.getRequestDispatcher("/fail.jsp").forward(request,response);
request.getRequestDispatcher("/fail.jsp")
.forward(request, response);
}
}
/**
}
DAO類
package Dao;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.concurrent.locks.Condition;
import org.apache.tomcat.jni.User;
import entity.Users;
public class UserDao {
public Users login(User condition)
{
Connection connection = ConnectionManager.getConnection();
Statement stmt = null;
Users user = null;
String sql = "select userid,username,password from tbl_user where username='"
+ user.getUname()
+ "' and password ='"
+ user.getUpass()
+ "'";
ResultSet rs = null;
try {
stmt = connection.createStatement();
rs = stmt.executeQuery(sql);
while (rs.next()) {
user = new Users();
int userid = rs.getInt("USERID");
String uname = rs.getString("UNAME");
String upass = rs.getString("UPASS");
int age = rs.getInt("AGE");
user.setUserid(userid);
user.setUname(uname);
user.setUpass(upass);
user.setAge(age);
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
ConnectionManager.closeResultSet(rs);
ConnectionManager.closeStatement(stmt);
ConnectionManager.closeConnection(connection);
}
return user;
}
}
我看出來了:
Users condition = new Users();
condition.setUname(uname);
condition.setUpass(upass);
UserDao userDao = new UserDao();
Users user = userDao.login(condition)就是這裡一直無法調用????????
這裡的userDao.login(condition);你的condition類型時Users,而你的dao層裡面login方法的參數:是import org.apache.tomcat.jni.User;
import org.apache.tomcat.jni.User;
import entity.Users;
public class UserDao {
public Users login(User condition)
類型出錯了,把你login方法裡的User改成Users就可以了