页面使用的是maven和spring开发的,文件结构如下
提示错误是因为jdk版本问题,对程序运行结果无影响。
login.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
Insert title here
hello.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
Insert title here
欢迎${user.name }登陆
web.xml
spring_login
login.jsp
org.springframework.web.context.ContextLoaderListener
contextConfigLocation
classpath:ioc.xml
LoginServlet
LoginServlet
com.bd.web.LoginServlet
LoginServlet
/loginServlet
pom.xml
4.0.0
com.wxl
spring_login
0.0.1-SNAPSHOT
war
org.springframework
spring-web
4.3.18.RELEASE
org.springframework
spring-jdbc
4.3.18.RELEASE
mysql
mysql-connector-java
5.1.18
org.springframework
spring-tx
4.3.18.RELEASE
org.aspectj
aspectjrt
1.6.11
org.aspectj
aspectjweaver
1.6.11
cglib
cglib
2.1
org.springframework
spring-beans
4.3.18.RELEASE
org.springframework
spring-core
4.3.18.RELEASE
org.springframework
spring-context
4.3.18.RELEASE
org.springframework
spring-expression
4.3.18.RELEASE
commons-logging
commons-logging
1.2
junit
junit
3.8.1
test
ioc.xml
db.properties
driverClass=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/stuinfomange
user=root
password=123456
LoginServlet.java
package com.bd.web;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
import com.bd.domain.User;
import com.bd.service.LoginService;
public class LoginServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
// servlet中不要直接使用注解自动获取对象,会出现空指针。为什么?不知道
//@Autowired
//LoginService loginService;
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// 创建ioc容器
ApplicationContext ioc = WebApplicationContextUtils.getWebApplicationContext(getServletContext());
// 获取用户的账号和密码
String username = request.getParameter("username");
String password = request.getParameter("password");
// @Component,@Controller,@Service,@Repository
// 自动创建对象,使用上述4个标签后,可直接获取对象
//为什么只能获取loginServiceImp的bean不能获取loginService?
LoginService loginService = (LoginService) ioc.getBean("loginServiceImp");
// 验证用户账号和密码
User user=loginService.select(username, password);
request.setAttribute("user", user);
if(user==null)
response.sendRedirect("login.jsp");
else request.getRequestDispatcher("hello.jsp").forward(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
LoginService.java
package com.bd.service;
import org.springframework.stereotype.Service;
import com.bd.domain.User;
@Service("loginService")
public interface LoginService {
User select(String username,String password);
}
LoginServiceImp.java
package com.bd.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.bd.dao.UserDao;
import com.bd.domain.User;
@Service
public class LoginServiceImp implements LoginService{
@Autowired
UserDao userDao;
public User select(String username,String password) {
return userDao.select(username, password);
}
}
UserDao.java
package com.bd.dao;
import org.springframework.stereotype.Component;
import com.bd.domain.User;
@Component
public interface UserDao {
User select(String username,String password);
}
UserDaoImp.java
package com.bd.dao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Component;
import com.bd.domain.User;
@Component
public class UserDaoImp implements UserDao{
@Autowired
JdbcTemplate jdbcTemplate;
public User select(String username,String password) {
RowMapper mapper = new BeanPropertyRowMapper<>(User.class);
User user=null;
try {
user = jdbcTemplate.queryForObject("select * from stuinfo where id=? and password=?", mapper,username,password);
} catch (Exception e) {
user=null;
}
return user;
}
}
User.java
package com.bd.domain;
import org.springframework.stereotype.Component;
@Component
public class User {
String id;
String name;
String addr;
String age;
String password;
int balance;
public User(String id, String name, String addr, String age, String password, int balance) {
super();
this.id = id;
this.name = name;
this.addr = addr;
this.age = age;
this.password = password;
this.balance = balance;
}
public int getBalance() {
return balance;
}
public void setBalance(int balance) {
this.balance = balance;
}
@Override
public String toString() {
return "User [id=" + id + ", name=" + name + ", addr=" + addr + ", age=" + age + ", password=" + password
+ ", balance=" + balance + "]";
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddr() {
return addr;
}
public void setAddr(String addr) {
this.addr = addr;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public User(String id, String name, String addr, String age, String password) {
super();
this.id = id;
this.name = name;
this.addr = addr;
this.age = age;
this.password = password;
}
public User() {
super();
}
}