1.如何在jsp中实现用户登录验证?
2.JSP ä¸ä¼ ä¸è½½ä»£ç
3.PHP实现一个账号同一时间只能一人登陆,用户p源源码给出源代码!登录
4.每次对jsp的模块码用请求都要将jsp转换为servlet吗?
5.求jsp登录源码 急急急急急急急急急急急
如何在jsp中实现用户登录验证?
1、新建TokenAction。户登2、录模配置struts.xml文件,用户p源源码天恒时时彩源码成功跳转至success.jsp. 重复的登录话跳转到 chongfu.jsp。
3、模块码用.新建token.jsp。户登
4、录模注意引入Struts2标签库,用户p源源码注意隐藏的登录token标签。
5、模块码用新建chongfu.jsp 重复提示。户登
6、录模访问token.jsp文件,输入姓名年龄,点击提交。
7、黄cms源码返回token.jsp页面,查看源代码.
JSP ä¸ä¼ ä¸è½½ä»£ç
æç»ä½ æï¼
1.é¦å ä¸è¿ä¸¤ä¸ªå commons-fileupload-1.2.1.jarï¼commons-io-1.3.2.jar
2.ç¼ååå°é¡µé¢
<%@ page language="java" pageEncoding="gbk"%>
<%@ taglib uri="/struts-tags" prefix="s" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4. Transitional//EN">
<html>
<body>
<form action="<%=request.getContextPath()%>/UploadServlet" method="post" enctype="multipart/form-data">
username:<input type="text" name="username"><br>
password:<input type="password" name="password"><br>
file:<input type="file" name="file"><br>
<input type="submit" value="submit"><br>
</form>
</body>
</html>
3.ç¼åservlet
package cn.jci.upload.servlet;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet..jci.upload.servlet.UploadServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>UploadServlet</servlet-name>
<url-pattern>/UploadServlet</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
5.åå¸
okäº
è¿æ¯æåå§çä¸ä¼ æ件çåæ³ï¼ç°å¨struts2ä¸ä¼ çè¯ï¼å°±è¦è½»æ¾å¥½å¤
PHP实现一个账号同一时间只能一人登陆,给出源代码!
对于一个帐号在同一时间只能一个人登录,可以通过下面的方法实现:
1 .在用户登录时,把用户添加到一个ArrayList中
2 .再次登录时查看ArrayList中有没有该用户,如果ArrayList中已经存在该用户,则阻止其登录
3 .当用户退出时,需要从该ArrayList中删除该用户,溯源码资讯这又分为三种情况
① 使用注销按钮正常退出
② 点击浏览器关闭按钮或者用Alt+F4退出,可以用javascript捕捉该页面关闭事件,
执行一段java方法删除ArrayList中的用户
③ 非正常退出,比如客户端系统崩溃或突然死机,可以采用隔一段时间session没活动就删除该session所对应的用户来解决,这样用户需要等待一段时间之后就可以正常登录。
在LoginAction中定义:
// 用来在服务器端存储登录的所有帐号
public static List logonAccounts;
login() 登录方法中:
// 设置session不活动时间为分
request.getSession().setMaxInactiveInterval(*);
if(logonAccounts==null){
logonAccounts = new ArrayList();
}
// 查看ArrayList中有没有该用户
for (int i = 0; i < logonAccounts.size(); i++) {
Account existAccount = (Account)logonAccounts.get(i);
if(account.getAccountId().equals(existAccount.getAccountId())){
return "denied";
}
}
// 在用户登录时,把sessionId添加到一个account对象中
// 在后面 ③ 需要根据此sessionId删除相应用户
account.setSessionId(request.getSession().getId());
// 该用户保存到ArrayList静态类变量中
logonAccounts.add(account);
return "login";
① 使用注销按钮正常退出
logout() 退出方法中:
if(logonAccounts==null){
logonAccounts = new ArrayList();
}
// 删除ArrayList中的用户 ⑴
for (int i = 0; i < logonAccounts.size(); i++) {
Account existAccount = (Account)logonAccounts.get(i);
if(account.getAccountId().equals(existAccount.getAccountId())){
logonAccounts.remove(account);
}
}
② 点击浏览器关闭按钮或者用Alt+F4退出:
在后台弹出一个窗口,在弹出窗口中删除ArrayList中的mycat的源码用户
function window.onbeforeunload(){
// 是否通过关闭按钮或者用Alt+F4退出
// 如果为刷新触发onbeforeunload事件,下面if语句不执行
if (event.clientX>document.body.clientWidth && event.clientY<0||event.altKey){
window.open('accountUnbound.jsp','',
'height=0,width=0,top=,left=')
}
}
accountUnbound.jsp : 弹出窗口中删除ArrayList中的用户
<%
Account account = (Account) request.getSession().getAttribute("account");
if(account != null){
if(LoginAction.logonAccounts==null){
LoginAction.logonAccounts = new ArrayList();
}
// 删除ArrayList中的用户——下面代码和上面的 ⑴ 处一样
for (int i = 0; i < logonAccounts.size(); i++) {
Account existAccount = (Account)logonAccounts.get(i);
if(account.getAccountId().equals(existAccount.getAccountId())){
logonAccounts.remove(account);
}
}
}
%>
为了保证上面代码可以执行完毕,3秒后关闭此弹出窗口(也位于accountUnbound.jsp中)
<script>
setTimeout("closeWindow();",);
function closeWindow(){
window.close();
}
</script>
③ 使LoginAction 实现implements HttpSessionListener,并实现sessionCreated,sessionDestroyed方法,在sessionDestroyed中删除ArrayList中的用户(用户超过分钟不活动则执行此方法)
public void sessionDestroyed(HttpSessionEvent event) {
// 取得不活动时的sessionId,并根据其删除相应logonAccounts中的用户
String sessionId = event.getSession().getId();
for (int i = 0; i < logonAccounts.size(); i++) {
Account existAccount = (Account)logonAccounts.get(i);
if(account.getSessionId().equals(existAccount.getSessionId())){
logonAccounts.remove(account);
}
}
}
注:
对于上面的,由于弹出窗口很容易被防火墙或者安全软件阻拦,造成无法弹出窗口,从而短时间不能登录,这种情况可以用AJAX来代替弹出窗口,cocos源码导入同样在后台执行删除用户的那段代码,却不会受到防火墙限制:
<script>
// <![CDATA[
var http_request = false;
function makeRequest(url) {
http_request = false;
if (window.XMLHttpRequest) { // Mozilla, Safari,...
http_request = new XMLHttpRequest();
if (http_request.overrideMimeType) {
http_request.overrideMimeType('text/xml');
}
} else if (window.ActiveXObject) { // IE
try {
http_request = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
http_request = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e) {
}
}
}
if (!http_request) {
alert('Giving up :( Cannot create an XMLHTTP instance');
return false;
}
http_request.onreadystatechange = alertContents;
http_request.open('GET', url, true);
http_request.send(null);
}
function alertContents() {
if (http_request.readyState == 4) {
if (http_request.status == ) {
window.close();
} else {
alert('There was a problem with the request.');
}
}
}
function window. onbeforeunload() {
makeRequest ('accountUnbound.jsp');
}
//]]>
</script>
每次对jsp的请求都要将jsp转换为servlet吗?
在处理动态网页请求时,如ASP、ASP.NET、JSP、PHP等,每次客户端对JSP的请求确实需要将其转换为Servlet。这是因为,JSP本质上是一种模板引擎,用于生成动态网页内容。它的源代码首先会被JSP引擎编译为Servlet,即一个Java类,这个过程发生在服务器端。Servlet作为Java的Web应用组件,能够执行Java代码,处理客户端请求并生成响应结果。因此,为了使JSP能够运行服务器端代码并生成动态网页内容,其源代码必须先转换为Servlet。
当用户请求一个JSP页面时,Web服务器(如Tomcat、Jetty等)接收到请求后,会调用JSP引擎来处理该请求。JSP引擎首先解析JSP页面的HTML和脚本元素,然后将这些元素转换为一个Java类,这个过程即编译阶段。在编译过程中,JSP引擎会检查JSP页面中是否存在脚本元素,并将它们转换为Java代码。然后,这个Java类会被JVM解释执行,生成动态内容,并最终以HTML格式返回给客户端浏览器。
简而言之,每次对JSP的请求都要将其转换为Servlet,这是因为JSP本身不具备直接执行服务器端代码的能力。通过将JSP源代码转换为Servlet,Web服务器能够执行Java代码,处理动态请求并生成响应内容。这一过程确保了动态网页能够根据用户请求生成个性化、动态的网页内容,从而实现丰富的Web应用功能。
求jsp登录源码 急急急急急急急急急急急
登陆页面 index.jsp源码:
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4. Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>login</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<form action="LoginServlet" method="post">
用户名:<input type="text" name="username" ><br>
密码:<input type="password" name="userpass"><br>
<input type="submit" value="登陆"> <input type="reset" value="取消">
</form>
</body>
</html>
-------------
LoginServlet.java 源码:
package servlet;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class LoginServlet extends HttpServlet {
/
*** Constructor of the object.
*/
public LoginServlet() {
super();
}
/
*** Destruction of the servlet. <br>
*/
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}
/
*** The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//获得jsp页面传输的参数
String username=request.getParameter("username");
String userpass=request.getParameter("userpass");
//判断
if(username.equals("user")&&userpass.equals("")){
response.sendRedirect("1.jsp");
}else if(username.equals("admin")&&userpass.equals("")){
response.sendRedirect("2.jsp");
}else{
response.sendRedirect("index.jsp");
}
}
/
*** The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to post.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.doGet(request, response);
}
/
*** Initialization of the servlet. <br>
*
* @throws ServletException if an error occurs
*/
public void init() throws ServletException {
// Put your code here
}
}
-------------
1.jsp:
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4. Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP '1.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
This is 1.jsp <br>
</body>
</html>
-------------
2.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4. Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP '1.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
This is 2.jsp <br>
</body>
</html>
2024-12-27 17:42
2024-12-27 17:38
2024-12-27 17:35
2024-12-27 17:24
2024-12-27 17:04
2024-12-27 16:53
2024-12-27 15:15
2024-12-27 15:09