如何链接JSP、servlet和Java视图bean?
如何链接JSP、servlet和Java视图bean? 我不想在JSP中有任何scriplet。如何让我的JSP调用servlet来设置隐式请求和响应对象,然后在JSP上显示视图bean中的属性 我需要将请求和响应对象从jsp传递到servlet,然后我想将视图bean中定义的属性显示到jsp页面 JSP->Servlet->View Bean->JSP 如何做到这一点 无需将请求和响应传递给bean/模型,因为您的模型类是在如何链接JSP、servlet和Java视图bean?,java,jsp,servlets,Java,Jsp,Servlets,如何链接JSP、servlet和Java视图bean? 我不想在JSP中有任何scriplet。如何让我的JSP调用servlet来设置隐式请求和响应对象,然后在JSP上显示视图bean中的属性 我需要将请求和响应对象从jsp传递到servlet,然后我想将视图bean中定义的属性显示到jsp页面 JSP->Servlet->View Bean->JSP 如何做到这一点 无需将请求和响应传递给bean/模型,因为您的模型类是在jsp或servlet中实例化的 您必须选择以servlet为中心的方
jsp
或servlet
中实例化的
您必须选择以servlet为中心的方法
ViewBean.java
ViewServlet.java
view.jsp
员工信息
ID:${emp.ID}
名称:${emp.Name}
index.jsp
无需将请求和响应传递给bean/model,因为您的模型类在jsp
或servlet
中实例化
您必须选择以servlet为中心的方法
ViewBean.java
ViewServlet.java
view.jsp
员工信息
ID:${emp.ID}
名称:${emp.Name}
index.jsp
这是如何。。。JSP、servlet和Java视图bean?
系列。请重新表述你的问题。有一些类似的问题这是如何。。。JSP、servlet和Java视图bean?
系列。请重新措辞你的问题。也有一些类似的感谢你的回答。但在servlet代码中,request.getRequestDispatcher(“/view.jsp”)。转发(请求、响应);代码给了我无法转发的消息。答复已经提交。错误。我通过@user1602657调用jsp中的servlet-您必须编辑您的问题并包含.jsps和servlet的代码。感谢您的回复。但在servlet代码中,request.getRequestDispatcher(“/view.jsp”)。转发(请求、响应);代码给了我无法转发的消息。答复已经提交。错误。我通过@user1602657调用jsp中的servlet-您必须编辑您的问题并包含.jsps和servlet的代码。
package in.abc.model;
public class Employee{
private Integer id;
private String name;
public Employee() { id=0; name="";}
public Employee(Integer id, String name) { this.id=id; this.name=name;}
public Integer getId() { return id;}
public String getName() { return name;}
public void setId(Integer id) { this.id=id;}
public void setName(String name) { this.name=name;}
}
package in.abc.servlets;
@WebServlet(name = "ViewServlet", urlPatterns = {"/view"})
public class ViewServlet extends HttpServlet{
public doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
//Instantiate the model
Employee emp=new Employee(10,"Mr.A");
//Insert "model" object to request
request.setAttribute("emp",emp);
//forward the request to view.jsp
request.getRequestDispatcher("/view.jsp").
forward(request,response);
}
public doPost(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
//Instantiate the model
Employee emp=new Employee();
//Read the request
try{
emp.setId(Integer.parseInt(request.getParameter("id"));
}catch(Exception ex) {}
emp.setName(request.getParameter("name"));
//Insert "model" object to request
request.setAttribute("emp",emp);
//forward the request to view.jsp
request.getRequestDispatcher("/view.jsp").
forward(request,response);
}
}
<h3>Employee info</h3>
<p>ID : ${emp.id}</p>
<p>Name : ${emp.name}</p>
<form method="post" action="view">
<br/> <input type="text" name="id"/>
<br/> <input type="text" name="name"/>
<br/> <input type="submit"/>
</form>
<h1>
<a href="view">View Employee details</a>
</h1>