添加:显示上次访问时间的cookies
This commit is contained in:
Generated
+1
@@ -1,5 +1,6 @@
|
|||||||
<?xml version="1.0" encoding="UTF-8"?>
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
<project version="4">
|
<project version="4">
|
||||||
|
<component name="GradleMigrationSettings" migrationVersion="1" />
|
||||||
<component name="GradleSettings">
|
<component name="GradleSettings">
|
||||||
<option name="linkedExternalProjectsSettings">
|
<option name="linkedExternalProjectsSettings">
|
||||||
<GradleProjectSettings>
|
<GradleProjectSettings>
|
||||||
|
|||||||
Generated
+38
-25
@@ -8,7 +8,8 @@
|
|||||||
</component>
|
</component>
|
||||||
<component name="ChangeListManager">
|
<component name="ChangeListManager">
|
||||||
<list default="true" id="4dd01e6d-ac98-4220-877b-dfc51a5bbe9c" name="更改" comment="">
|
<list default="true" id="4dd01e6d-ac98-4220-877b-dfc51a5bbe9c" name="更改" comment="">
|
||||||
<change beforePath="$PROJECT_DIR$/.idea/misc.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/misc.xml" afterDir="false" />
|
<change afterPath="$PROJECT_DIR$/src/main/java/com/msksbr/test/servlet/servlet/cookies/LastAccessServlet.java" afterDir="false" />
|
||||||
|
<change beforePath="$PROJECT_DIR$/../../homework2/.idea/gradle.xml" beforeDir="false" afterPath="$PROJECT_DIR$/../../homework2/.idea/gradle.xml" afterDir="false" />
|
||||||
<change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
|
<change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
|
||||||
</list>
|
</list>
|
||||||
<option name="SHOW_DIALOG" value="false" />
|
<option name="SHOW_DIALOG" value="false" />
|
||||||
@@ -21,6 +22,13 @@
|
|||||||
<ProjectState />
|
<ProjectState />
|
||||||
</projectState>
|
</projectState>
|
||||||
</component>
|
</component>
|
||||||
|
<component name="FileTemplateManagerImpl">
|
||||||
|
<option name="RECENT_TEMPLATES">
|
||||||
|
<list>
|
||||||
|
<option value="Class" />
|
||||||
|
</list>
|
||||||
|
</option>
|
||||||
|
</component>
|
||||||
<component name="FormatOnSaveOptions">
|
<component name="FormatOnSaveOptions">
|
||||||
<option name="myRunOnSave" value="true" />
|
<option name="myRunOnSave" value="true" />
|
||||||
</component>
|
</component>
|
||||||
@@ -41,35 +49,39 @@
|
|||||||
"associatedIndex": 5
|
"associatedIndex": 5
|
||||||
}</component>
|
}</component>
|
||||||
<component name="ProjectId" id="2uCvpUspkjjEe0ld0gTakT89nEb" />
|
<component name="ProjectId" id="2uCvpUspkjjEe0ld0gTakT89nEb" />
|
||||||
|
<component name="ProjectLevelVcsManager">
|
||||||
|
<ConfirmationsSetting value="2" id="Add" />
|
||||||
|
</component>
|
||||||
<component name="ProjectViewState">
|
<component name="ProjectViewState">
|
||||||
<option name="hideEmptyMiddlePackages" value="true" />
|
<option name="hideEmptyMiddlePackages" value="true" />
|
||||||
<option name="showLibraryContents" value="true" />
|
<option name="showLibraryContents" value="true" />
|
||||||
</component>
|
</component>
|
||||||
<component name="PropertiesComponent">{
|
<component name="PropertiesComponent"><![CDATA[{
|
||||||
"keyToString": {
|
"keyToString": {
|
||||||
"Gradle.构建 servlet.executor": "Run",
|
"Gradle.构建 servlet.executor": "Run",
|
||||||
"RunOnceActivity.ShowReadmeOnStart": "true",
|
"RunOnceActivity.ShowReadmeOnStart": "true",
|
||||||
"RunOnceActivity.git.unshallow": "true",
|
"RunOnceActivity.git.unshallow": "true",
|
||||||
"Tomcat 服务器.Tomcat 11.0.51.executor": "Run",
|
"Tomcat 服务器.Tomcat 11.0.51.executor": "Run",
|
||||||
"code.cleanup.on.save": "true",
|
"code.cleanup.on.save": "true",
|
||||||
"git-widget-placeholder": "master",
|
"git-widget-placeholder": "master",
|
||||||
"kotlin-language-version-configured": "true",
|
"kotlin-language-version-configured": "true",
|
||||||
"node.js.detected.package.eslint": "true",
|
"node.js.detected.package.eslint": "true",
|
||||||
"node.js.detected.package.tslint": "true",
|
"node.js.detected.package.tslint": "true",
|
||||||
"node.js.selected.package.eslint": "(autodetect)",
|
"node.js.selected.package.eslint": "(autodetect)",
|
||||||
"node.js.selected.package.tslint": "(autodetect)",
|
"node.js.selected.package.tslint": "(autodetect)",
|
||||||
"nodejs_package_manager_path": "npm",
|
"nodejs_package_manager_path": "npm",
|
||||||
"project.structure.last.edited": "模块",
|
"project.structure.last.edited": "模块",
|
||||||
"project.structure.proportion": "0.0",
|
"project.structure.proportion": "0.0",
|
||||||
"project.structure.side.proportion": "0.0",
|
"project.structure.side.proportion": "0.0",
|
||||||
"rearrange.code.on.save": "true",
|
"rearrange.code.on.save": "true",
|
||||||
"run.code.analysis.last.selected.profile": "pProject Default",
|
"run.code.analysis.last.selected.profile": "pProject Default",
|
||||||
"settings.editor.selected.configurable": "actions.on.save",
|
"settings.editor.selected.configurable": "reference.settingsdialog.IDE.editor.postfix.templates",
|
||||||
"vue.rearranger.settings.migration": "true"
|
"vue.rearranger.settings.migration": "true"
|
||||||
}
|
}
|
||||||
}</component>
|
}]]></component>
|
||||||
<component name="RunManager">
|
<component name="RunManager">
|
||||||
<configuration name="Tomcat 11.0.51" type="#com.intellij.j2ee.web.tomcat.TomcatRunConfigurationFactory" factoryName="Local" APPLICATION_SERVER_NAME="Tomcat 11.0.51" ALTERNATIVE_JRE_ENABLED="false">
|
<configuration name="Tomcat 11.0.51" type="#com.intellij.j2ee.web.tomcat.TomcatRunConfigurationFactory" factoryName="Local" APPLICATION_SERVER_NAME="Tomcat 11.0.51" ALTERNATIVE_JRE_ENABLED="false">
|
||||||
|
<option name="OPEN_IN_BROWSER_URL" value="http://localhost:8080/Gradle___com_msksbr_test_servlet___servlet_1_0_SNAPSHOT_war" />
|
||||||
<option name="UPDATING_POLICY" value="hot-swap-classes" />
|
<option name="UPDATING_POLICY" value="hot-swap-classes" />
|
||||||
<deployment>
|
<deployment>
|
||||||
<artifact name="Gradle : com.msksbr.test.servlet : servlet-1.0-SNAPSHOT.war">
|
<artifact name="Gradle : com.msksbr.test.servlet : servlet-1.0-SNAPSHOT.war">
|
||||||
@@ -161,8 +173,8 @@
|
|||||||
<component name="SharedIndexes">
|
<component name="SharedIndexes">
|
||||||
<attachedChunks>
|
<attachedChunks>
|
||||||
<set>
|
<set>
|
||||||
<option value="bundled-jdk-9823dce3aa75-a94e463ab2e7-intellij.indexing.shared.core-IU-243.25659.59" />
|
<option value="bundled-jdk-9823dce3aa75-a94e463ab2e7-intellij.indexing.shared.core-IU-243.26053.27" />
|
||||||
<option value="bundled-js-predefined-d6986cc7102b-1632447f56bf-JavaScript-IU-243.25659.59" />
|
<option value="bundled-js-predefined-d6986cc7102b-1632447f56bf-JavaScript-IU-243.26053.27" />
|
||||||
</set>
|
</set>
|
||||||
</attachedChunks>
|
</attachedChunks>
|
||||||
</component>
|
</component>
|
||||||
@@ -177,6 +189,7 @@
|
|||||||
<workItem from="1741765555514" duration="727000" />
|
<workItem from="1741765555514" duration="727000" />
|
||||||
<workItem from="1742184110126" duration="1297000" />
|
<workItem from="1742184110126" duration="1297000" />
|
||||||
<workItem from="1742185427721" duration="1266000" />
|
<workItem from="1742185427721" duration="1266000" />
|
||||||
|
<workItem from="1742790706226" duration="2930000" />
|
||||||
</task>
|
</task>
|
||||||
<servers />
|
<servers />
|
||||||
</component>
|
</component>
|
||||||
|
|||||||
+67
@@ -0,0 +1,67 @@
|
|||||||
|
package com.msksbr.test.servlet.servlet.cookies;
|
||||||
|
|
||||||
|
import jakarta.servlet.ServletException;
|
||||||
|
import jakarta.servlet.annotation.WebServlet;
|
||||||
|
import jakarta.servlet.http.Cookie;
|
||||||
|
import jakarta.servlet.http.HttpServlet;
|
||||||
|
import jakarta.servlet.http.HttpServletRequest;
|
||||||
|
import jakarta.servlet.http.HttpServletResponse;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.net.URLDecoder;
|
||||||
|
import java.net.URLEncoder;
|
||||||
|
import java.text.SimpleDateFormat;
|
||||||
|
import java.util.Date;
|
||||||
|
|
||||||
|
@WebServlet(
|
||||||
|
name = "lastAccessServlet",
|
||||||
|
urlPatterns = "/cookies/last-access-servlet"
|
||||||
|
)
|
||||||
|
public class LastAccessServlet extends HttpServlet {
|
||||||
|
private static final long serialVersionUID = 1L;
|
||||||
|
|
||||||
|
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
|
||||||
|
response.setContentType("text/html;charset=UTF-8");
|
||||||
|
Cookie[] cookies = request.getCookies();
|
||||||
|
boolean flag = false;
|
||||||
|
if (cookies.length > 0 && cookies != null) {
|
||||||
|
for (Cookie cookie : cookies) {
|
||||||
|
String name = cookie.getName();
|
||||||
|
if ("lastTime".equals(name)) {
|
||||||
|
flag = true;
|
||||||
|
String value = cookie.getValue();
|
||||||
|
System.out.println("解码前:" + value);
|
||||||
|
value = URLDecoder.decode(value, "UTF-8");
|
||||||
|
cookie.setValue("解码后:" + value);
|
||||||
|
response.getWriter().write("欢迎回来,您上次访问时间" + "为:" + value);
|
||||||
|
Date date = new Date();
|
||||||
|
SimpleDateFormat timesdf = new SimpleDateFormat("yyyy年MM" + "月dd日 HH:mm:ss");
|
||||||
|
String str_time = timesdf.format(date);
|
||||||
|
System.out.println("编码前:" + str_time);
|
||||||
|
str_time = URLEncoder.encode(str_time, "UTF-8");
|
||||||
|
System.out.println("编码后:" + str_time);
|
||||||
|
cookie.setValue(str_time);
|
||||||
|
cookie.setMaxAge(60 * 60 * 24 * 30);
|
||||||
|
response.addCookie(cookie);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (cookies == null || cookies.length == 0 || !flag) {
|
||||||
|
Date date = new Date();
|
||||||
|
SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日" + "HH:mm:ss");
|
||||||
|
String str_date = sdf.format(date);
|
||||||
|
System.out.println("编码前:" + str_date);
|
||||||
|
str_date = URLEncoder.encode(str_date, "UTF-8");
|
||||||
|
System.out.println("编码后:" + str_date);
|
||||||
|
Cookie cookie = new Cookie("lastTime", str_date);
|
||||||
|
cookie.setMaxAge(60 * 60 * 24 * 30);
|
||||||
|
response.addCookie(cookie);
|
||||||
|
response.getWriter().write("您好,欢迎您首次访问");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
|
||||||
|
this.doPost(request, response);
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user