gpt4 book ai didi

javascript - 在 Javascript 中使用 JSP 值

转载 作者:行者123 更新时间:2023-11-27 23:36:42 24 4
gpt4 key购买 nike

所以我有一个聊天系统,目前可以正常登录和注册,但即使用户登录,我仍然需要他们输入昵称来聊天。

我想改变这一点,只使用他们当前登录的登录名,我只是在右上角打印登录名,我不知道谁在我的 JavaScript 中使用它

下面的当前代码仅使用从登录表单中获取的值昵称,我想使用 user_id 来代替,并让 jaavscript 选择它

这是 Home.jsp

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Soc Talk Home Page</title>
<script src="resource/js/jquery-1.10.2.min.js"></script>
<link rel="stylesheet" href="resource/css/bootstrap.min.css" />
<link rel="stylesheet" href="resource/css/style.css" />
<script>
var wsocket;
var serviceLocation = "ws://" + document.location.host + "/SocTalk/chat/";
var $nickName;
var $message;
var $chatWindow;
var room = '';

function _MessageRecived(evt) {
var msg = JSON.parse(evt.data);//API
var $messageLine = $('<tr><td class="received">' + msg.received + '</td><td class="sender">' + msg.sender + '</td><td class="message">' + msg.message + '</td></tr>');
$chatWindow.append($messageLine);
}
function _MessageSending() {
var msg = '{"message":"' + $message.val() + '", "sender":"'
+ $nickName.val() + '", "received":""}';
wsocket.send(msg);
$message.val('').focus();
}

function _ConnectToAChatServer() {
room = $('#chatroom option:selected').val();
wsocket = new WebSocket(serviceLocation + room);
wsocket.onmessage = _MessageRecived;
}

function _ExitRoom() {
wsocket.close();
$chatWindow.empty();
$('.chat-wrapper').hide();
$('.chat-signin').show();
$nickName.focus();
}

$(document).ready(function () {
$nickName = $('#nickname');
$message = $('#message');
$chatWindow = $('#response');
$('.chat-wrapper').hide();
$nickName.focus();

$('#enterRoom').click(function (evt) {
evt.preventDefault();
_ConnectToAChatServer();
$('title').text('@' + room);
$('.chat-wrapper h2').text('Chatroom @' + room);
$('.chat-wrapper h4').text('Currently logged in ' + $nickName.val());
$('.chat-signin').hide();
$('.chat-wrapper').show();
$message.focus();
});
$('#do-chat').submit(function (evt) {
evt.preventDefault();
_MessageSending()
});

$('#Exit-room').click(function () {
_ExitRoom();
});
});

</script>
</head>
<body id="Body">
<%@ page import ="java.sql.*" %><%@ page import ="javax.sql.*" %><%String user = request.getParameter("userid");
session.putValue("userid", user);
String pwd = request.getParameter("pwd");
Class.forName("com.mysql.jdbc.Driver");
java.sql.Connection con = DriverManager.getConnection("jdbc:mysql://localhost:8081/socusers", "root", "");
Statement st = con.createStatement();
ResultSet rs = st.executeQuery("select * from users where user_id='" + user + "'");
String message = "";
if (rs.next()) {
if (rs.getString(2).equals(pwd)) {
message = "Welcome " + user;
} else {
message = "Invalid password try again";
//Redirect after 3 seconds back to login
}
} else {
message = "Invalid user try again";
//Redirect after 3 seconds back to login
}
out.println(message);


%>

<center>
<div class="container chat-signin">
<form class="form-signin">
<h2><i>NCI Soc Talk</i></h2>
<label for="nickname">Enter Nickname:</label> <input type="text" placeholder="Nickname" id="nickname">
<div>
<label for="chatroom">Select Chatroom</label> <select size="1" id="chatroom">
<option value="1">Gaming Soc</option>
<option value="2">Pokemon Soc</option>
<option value="3">Fashion Soc</option>
<option value="4">Other Soc</option>
</select>
</div>
<button type="submit" id="enterRoom">Sign in</button>
</form>
</div>

<div class="container chat-wrapper">
<form id="do-chat">
<h2></h2>
<h4></h4>
<table id="response" ></table>
<fieldset>
<legend>Enter your message..</legend>
<div>
<input type="text" placeholder="Your message..." id="message" style="height:60px; width:1000px;"/>
<br />
<input type="submit" value="Send message" />
<button type="button" id="Exit-room">Exit Room</button>
</div>
</fieldset>
</form>
</div>
</center>
</body>
</html>

最佳答案

请记住,JSP 只是编写 servlet 的一种奇特方式,整个事物就是一个模板。获取添加到正文中的 Java 代码(从 <%@ 到 out.println(message) 下的 %> 结束标记,并将其放在文件的第一行(<%@page 行)下方。删除 out.println(message) 行。

然后,在您的 <script> 中 block ,您可以分配值user到 JavaScript 变量:

var user = "<%= user %>";

请小心并 sanitizer user ,确保其中没有引号或反斜杠。

关于javascript - 在 Javascript 中使用 JSP 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34076701/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com