【发布时间】:2020-01-31 07:55:25
【问题描述】:
指向那里的箭头我需要输出姓氏是:lion
我无法获得欲望输出。我认为问题出在 index.jsp 或 NameController.java 文件中
索引jsp代码
<form name="greetingForm" action="NameController" method="post" style="width: 300px; ">
<table>
<tr>
<td>Please enter your name</td>
</tr>
<tr>
<td><input name="name" value='${nameForm.name}'/></td>
</tr>
<tr>
<td>Enter Last name</td>
</tr>
<tr>
<td><input name="lastName" value='${nameForm.lastName }'/></td>
<td><input type="submit" value="Submit"></td>
</tr>
</table>
</form>
<input id="greetingField" value='${nameForm.greetingText}' style ="background-color: white; border: none; width: 400px;" disabled="disabled" />
<input id="greetingFields" value='${val.idText }' style ="background-color: white; border: none; width: 400px;"disabled = "disabled" />
<input id="currentTime" value='${nameForm.currentTime}' style ="background-color: white; border: none; width: 400px;" disabled="disabled" />
</table>
</body>
</html>
NameForm.java
public void setCurrentTime(String currentTime) {
this.currentTime = currentTime;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
//my code
public String getlastName() {
return lastName;
}
public void setlastName(String lastName) {
this.lastName = lastName;
}
//my code ends
public String getGreetingText() {
return greetingText;
}
public void setGreetingText(String greetingText) {
this.greetingText = greetingText;
}
//my code
public String getIdText() {
return idText;
}
public void setIdText(String idText) {
this.idText = idText;
}
//my code ends
}
名称 controller.java
public class NameController extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
HttpSession session = request.getSession();
NameForm nameForm = new NameForm();
NameForm val = new NameForm();
nameForm.setName(request.getParameter("name"));
val.setlastName(request.getParameter("lastName"));
session.setAttribute("nameForm",nameForm);
session.setAttribute("val", val);
nameForm.setGreetingText("Hello "+nameForm.getName());
val.setGreetingText("last name is:" +val.getlastName());
getServletContext().getRequestDispatcher("/index.jsp").forward(request, response);
}
}
【问题讨论】:
-
在此处以文本形式发布您的代码。
-
如果你点击那个链接,你可以看到代码,但我还是会把它贴在这里
-
@AdityaBhogte 您已经使用
val在您的servlet 中为lastname设置值。所以您需要使用val.lastName来访问它。但是您这里有错字而不是@987654330 @你已经使用${nameForm.lastName}检查你的输入并相应地改变它。
标签: java jsp servlets textbox output