Javaweb创建ReadPropertyServlet,使用servletContext对象读取application.properties中的学院与班级信息并输出到控制台中,登录成功后显示上次登录时间(利用cookie技术)
时间: 2024-04-30 21:23:38 浏览: 105
JavaWeb连接数据库(Dao模式),实现添加book并按日期范围查找相应book信息
首先,需要在项目的src目录下创建一个application.properties文件,并在文件中添加如下内容:
```
college=计算机学院
class=计算机科学与技术1班
```
然后,创建一个ReadPropertyServlet类,继承HttpServlet类,实现读取application.properties文件中学院和班级信息的功能。代码如下:
```
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class ReadPropertyServlet extends HttpServlet {
@Override
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
ServletContext context = this.getServletContext();
InputStream is = context.getResourceAsStream("/WEB-INF/classes/application.properties");
Properties prop = new Properties();
prop.load(is);
String college = prop.getProperty("college");
String clazz = prop.getProperty("class");
System.out.println("学院:" + college + ", 班级:" + clazz);
}
}
```
接下来,需要实现利用cookie技术显示上次登录时间的功能。我们可以在登录成功后,在response中设置一个名为“lastLoginTime”的cookie,值为当前时间。然后,在下次登录时可以从request中获取这个cookie,并显示上次登录时间。代码如下:
```
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class LoginServlet extends HttpServlet {
@Override
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String username = request.getParameter("username");
String password = request.getParameter("password");
if (username.equals("admin") && password.equals("admin")) {
// 登录成功,设置cookie
Cookie cookie = new Cookie("lastLoginTime", String.valueOf(System.currentTimeMillis()));
response.addCookie(cookie);
response.sendRedirect("index.jsp");
} else {
// 登录失败,返回登录页面
response.sendRedirect("login.jsp");
}
}
}
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class IndexServlet extends HttpServlet {
@Override
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// 获取上次登录时间的cookie
Cookie[] cookies = request.getCookies();
String lastLoginTime = null;
if (cookies != null) {
for (Cookie cookie : cookies) {
if (cookie.getName().equals("lastLoginTime")) {
lastLoginTime = cookie.getValue();
break;
}
}
}
// 输出上次登录时间
if (lastLoginTime != null) {
System.out.println("上次登录时间:" + lastLoginTime);
} else {
System.out.println("第一次登录");
}
}
}
```
以上就是利用servletContext对象读取application.properties中的学院与班级信息,并利用cookie技术显示上次登录时间的完整代码。
阅读全文