首先本次使用的Java编译器为Idea,所以使用esclipse编译可能需要后期修改,本次代码需要各位看官重新建立项目,防止端口重复的错误。
上代码
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 java.io.IOException;
import java.io.PrintWriter;
@WebServlet("/s08")
public class Servlet08 extends HttpServlet {
@Override
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
request.setCharacterEncoding("UTF-8");
PrintWriter out = response.getWriter();
//获取用户输入信息
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("UTF-8");
String year1 = request.getParameter("username");
String month1 = request.getParameter("userpwd");
String day1 = request.getParameter("cuserpwd");
Integer year = Integer.parseInt(year1);
Integer month = Integer.parseInt(month1);
Integer day = Integer.parseInt(day1);
int y = year;
int m = month;
int d = day;
int i = 0;
if (year >= 2050 || year <= 1920|| month <= 0 || month >= 13|| day <= 0)
i++;
switch (month) {
case 1 :
case 3 :
case 5 :
case 7 :
case 8 :
case 10 :{
if (day < 31)
d = day + 1;
else if (day == 31) {
m = month + 1;
d = 1;
}
else
i++;
break;
}
case 12 :{
if (day < 31)
d = day + 1;
else if (day == 31) {
y = year + 1;
m = 1;
d = 1;
}
else
i++;
break;
}
case 4:
case 6:
case 9:
case 11:{
if (day < 30)
d = day + 1;
else if (day == 30) {
m = month + 1;
d = 1;
}
else
i++;
break;
}
case 2:{
if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) {
if (day < 29)
d = day + 1;
else if (day == 29) {
m = 3;
d = 1;
}
else
i++;
break;
}
else {
if (day < 28)
d = day + 1;
else if (day == 28) {
m = 3;
d = 1;
}
else
i++;
break;
}
}
}
if (i != 0) {
out.print("无效的日期");
}
else {
out.print("下一日日期:"+y+"."+m+"."+d);
}
out.print(" " + " " + " " + " " + "<a href=\"admin.jsp\">返回计算首页 </ a>");
}
}
<%--
Created by IntelliJ IDEA.
User: ${陈平安}
Date: 2022/6/4
Time: 11:26
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<pre>
<script type="text/javascript">
// function refresh(){
// loginForm.imgValidate.src = "validate.jsp?id=" + Math.random();
// }
</script>
<%--<h2 style="text-align: center " >个人云博客管理员入口</h2><br>--%>
<form style="text-align: center" method="post" action="s08" >
输入年:<input type="text" name="username"><br>
输入月:<input type="text" name="userpwd"><br>
确认日:<input type="text" name="cuserpwd"><br>
<%-- 请输入验证码:<input type="text" name="code" size="10">--%>
<%-- <img name="imgValidate" src="filter01/validate.jsp" onclick="refresh()"><BR>--%>
<input type="submit" value="计算">
</form>
</pre>
</body>
</html>
结果输出:
后期代码测试,稍后