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

如何将URL参数值传递到网页链接?求非JavaScript实现方法

Hey there! Since you want to avoid JavaScript entirely, we’ll need to handle this on the server side—this is the standard approach for passing URL parameters without relying on client-side code. Below are practical solutions for some of the most common backend frameworks/languages:

Java (Servlet/JSP)

If you're using Java with Servlets and JSP, you can directly access the URL parameter in your JSP page using Expression Language (EL):

<a href="localhost:8080/testsite/viewinfo?person=${param.name}">View info</a>

For a more structured approach (using a Servlet to handle logic first), you can fetch the parameter in your Servlet code, pass it to the JSP as an attribute, then use it in the link:

// Inside your Servlet's doGet() method
String personName = request.getParameter("name");
request.setAttribute("personName", personName);
request.getRequestDispatcher("/testsite/person.jsp").forward(request, response);

Then in your JSP:

<a href="localhost:8080/testsite/viewinfo?person=${personName}">View info</a>

Pro tip: To prevent XSS attacks, use JSTL's fn:escapeXml to sanitize the value:

<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<a href="localhost:8080/testsite/viewinfo?person=${fn:escapeXml(param.name)}">View info</a>
Python (Flask)

In Flask, you'll grab the name parameter from the request in your route function, then pass it to your HTML template:

# app.py
from flask import Flask, request, render_template

app = Flask(__name__)

@app.route('/testsite/person')
def person_page():
    person_name = request.args.get('name')
    return render_template('person.html', person_name=person_name)

Then in your person.html template:

<a href="localhost:8080/testsite/viewinfo?person={{ person_name }}">View info</a>

Flask's Jinja2 templates automatically escape variables by default, so you're protected from basic XSS risks here.

Python (Django)

For Django, fetch the parameter in your view function and pass it to the template context:

# views.py
from django.shortcuts import render

def person_view(request):
    person_name = request.GET.get('name')
    return render(request, 'person.html', {'person_name': person_name})

Then in your template:

<a href="localhost:8080/testsite/viewinfo?person={{ person_name }}">View info</a>

Like Flask, Django templates auto-escape variables to help prevent XSS attacks.

PHP

In PHP, you can directly insert the name parameter into the link using the $_GET superglobal:

<a href="localhost:8080/testsite/viewinfo?person=<?php echo htmlspecialchars($_GET['name']); ?>">View info</a>

Always use htmlspecialchars() here to sanitize the input and avoid XSS vulnerabilities.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.06 10:09:07