【发布时间】:2017-05-20 23:22:35
【问题描述】:
我正在尝试从 url 获取 id 但 getParameter 返回 null 这就是我在 url (tool.jsp) 中发送 id 的方式:
<a href="http://localhost:8080/Projectpfeweb/executetool.jsp?id=${l.tool_id}" class="btn btn-info" role="button">Execute</a>
这是我想要 id 值的 doGet 方法
protected void doGet(HttpServletRequest req,HttpServletResponse res) throws ServletException,IOException
{
ToolDAO dao=new ToolDAO();
String id= req.getParameter("id");
Tool t=dao.getToolById(Integer.parseInt(id));
String first = req.getParameter("first");
byte[] bytes = first.getBytes(StandardCharsets.ISO_8859_1);
first= new String(bytes, StandardCharsets.UTF_8);
if(first!=null)
{
String [] input=first.split(" ");
System.out.println("input"+input[0]);
EXEJAVA exe=new EXEJAVA();
FileRead f=new FileRead();
f.writeinputfile(input);
ArrayList l=exe.executetool(t.getTool_path_exe());
req.setAttribute("l",l);
req.setAttribute("first", first);
req.getRequestDispatcher("executetool.jsp").forward(req, res);
}
这是表单(executetool.jsp)
<form accept-charset="UTF-8" name="form" action="executetool" method="get">
<div class="centre">
<div class="row">
<div class="inline-div">
<label for="ta1">Text in Latin script</label>
<textarea cols="60" rows="15" id="first" name="first" class="inline-
txtarea">${first}</textarea>
</div>
<div class="inline-div">
<input type="button" value="Clear" onclick="javascript:eraseText();">
</div>
<div class="inline-div">
<label for="ta2" >Text in Arabic script</label>
<textarea cols="60" rows="15" id="second" name="second" class="inline-
txtarea"><c:forEach items="${l}" var="s">${s} </c:forEach>
</textarea>
</div>
</div>
</div>
</form>
由于它的方法 get 每次刷新页面时 url 都会不断变化,因此“id=something”部分被替换为我在表单中拥有的两个文本区域的值在网址中?
【问题讨论】:
-
对ID属性使用隐藏输入或将ID设置为会话,您可以从任何地方访问参数。
-
隐藏输入解决方案不起作用,因为如果再次刷新页面,值会丢失,至于我不熟悉的会话,请您给我更多详细信息吗?跨度>
-
IN JSP :
-
在 SERVLET 或 ETC 中。 public void doGet(HttpServletRequest request, HttpServletResponse response) { HttpSession session = request.getSession(); String username = (String)request.getAttribute("un"); session.setAttribute("用户名", 用户名); }
-
没用,我得到了 null