You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在动态JSP页面中获取URL中的employee_id并设置为不可编辑输入框

解决URL中提取employee_id并放入不可编辑文本框的问题

嘿,我来帮你搞定这个问题!你之前用session.getAttribute("employee_id")没成功,大概率是因为这个employee_id压根没被存进session里——毕竟它是直接挂在URL参数里的嘛。下面给你两种靠谱的解决办法,按需选就行:

方法一:JSP后端直接获取(推荐,更安全)

既然是JSP页面,咱们可以直接从请求参数里拿URL上的employee_id,一步到位放到不可编辑的文本框里,代码示例如下:

<html>
<body>
<form>
    <!-- 不可编辑文本框,直接从请求参数取值 -->
    <input type="text" name="employee_id" 
           value="<%= request.getParameter("employee_id") != null ? request.getParameter("employee_id") : "" %>" 
           readonly>
</form>
</body>
</html>

这里用request.getParameter("employee_id")直接抓取URL里的参数,三元表达式是为了避免参数为空时页面显示null。加上readonly属性就能让用户没法编辑这个输入框啦。

要是你之后需要在其他页面用这个employee_id,可以在JSP开头先把它存进session:

<%
String empId = request.getParameter("employee_id");
if(empId != null && !empId.trim().isEmpty()){
    session.setAttribute("employee_id", empId);
}
%>

这样之后再用session.getAttribute("employee_id")就能拿到值了。

方法二:前端JavaScript解析URL获取

如果想纯前端处理,写个小函数解析URL参数就行,代码如下:

<html>
<body>
<form>
    <input type="text" id="empIdInput" readonly>
</form>

<script>
// 封装解析URL参数的函数
function getUrlParameter(paramName) {
    paramName = paramName.replace(/[\[]/, '\\[').replace(/[\]]/, '\\]');
    const regex = new RegExp('[\\?&]' + paramName + '=([^&#]*)');
    const results = regex.exec(window.location.search);
    return results === null ? '' : decodeURIComponent(results[1].replace(/\+/g, ' '));
}

// 页面加载完成后给文本框赋值
window.addEventListener('load', function() {
    const empId = getUrlParameter('employee_id');
    document.getElementById('empIdInput').value = empId;
});
</script>
</body>
</html>

这个getUrlParameter函数会自动解析当前页面URL里的查询参数,拿到employee_id的值后,在页面加载完成时赋值给文本框,同样用readonly属性锁定编辑权限。

内容的提问来源于stack exchange,提问作者Aties

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 08:04:27