先打算参考这篇文章
Simple login page example using jsp servlet - Candidjava
内含war文件和zip文件。但是tomcat上该war文件总是报错。
于是又参考这篇
(46条消息) Java学习day3——Javaweb登录页面设计(1)(含JSP代码)_苏三有春的博客-CSDN博客_web登录界面设计代码
复制代码,略加修改,没有使用war,使用纯JSP.
写了3个jsp
loginSuccess.jsp
<%@ page language="java" contentType="text/html; charset=UTF=8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>loginSuccess</title>
</head>
<body>
<%
request.setCharacterEncoding("UTF-8");//解决提交方式post时,中文乱码问题
String userName = null;
String userPwd = null;
if(request.getParameter("userName")!=null){
userName=request.getParameter("userName");
userPwd = request.getParameter("password");
}
if(userName.equals("admin")&&userPwd.equals("123")){
//当用户名和密码正确时
request.setAttribute("mess", "登陆成功");
session.setAttribute("userName", userName);
session.setAttribute("userPwd",userPwd);
//进入content.jsp页面
response.sendRedirect("content.jsp");
}else{
//当用户名或密码不正确时
request.setAttribute("mess", "用户名或密码错误");
request.getRequestDispatcher("index.jsp").forward(request, respo
nse);
}
%>
<h3>userName:<%= userName %></h3>
<h3>password:<%= request.getParameter("password") %></h3>
</body>
</html>
index.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>登录页面</title>
</head>
<body>
<%
String mess = null;
if(request.getAttribute("mess")!=null){
mess = (String)request.getAttribute("mess");
}
%>
<h3>input admin and 123</h3>
<form method="post" action="loginSuccess.jsp">
<label>用户名:</label>
<input type="text" name="userName" ><br>
<label>密码:</label>
<input type="password" name="password" ><br>
<input type="submit" value="登录" >
<p sytle="color: red"><%= mess != null ? mess : "" %></p>
</form>
</body>
</html>
content.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Index page, login success</title>
</head>
<body>
<%
String userName = (String)session.getAttribute("userName");
String userPwd = (String)session.getAttribute("userPwd");
session.setMaxInactiveInterval(3*60);//设置session失效时间
%>
<h1>恭喜你登录成功</h1>
<h1>欢迎合法用户: <%= userName != null ? userName : ""%></h1>
<p>sessionId: <%= session.getId() %></p>
</body>
</html>
然后打包成xxx.tar.gz文件
再制作Dockerfile
FROM tomcat
LABEL maintainer="admin@soft.com"
ADD root.tar.gz /usr/local/tomcat/webapps/
EXPOSE 8080
CMD ["/usr/local/tomcat/bin/catalina.sh", "run"]
再参考Deploying Your First Web App to Tomcat on Docker | Cprime
制作和运行docker