Sending and Retreiving data from JSP to Serverlet and visversa
Note :- UI to Servlet - html or jsp
- Servelet to UI - jsp only
UI to Servlet
Login.html
<html>
<body>
<form action="MyServerlet1" method="get">
<p>Username <input name="Usn" type="text" /> </p>
<p>Password <input name="Password" type="text" /></p>
<p><input name="Submit" type="submit" value="Submit" /></p>
</form>
</body>
</html>
MyServerlet1.java
- Project>Javarespurces>src>package>rt ck >new servelet>
- Name "MyServerlet1" >Next,next>check doGet and doPost>Finish
- Add below code to retreieve the data from "Usn" and "Password" fields.
public class MyServerlet1 extends HttpServlet {
public MyServerlet1() {
super();
// TODO Auto-generated constructor stub
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
PrintWriter out=response.getWriter();
String username = request.getParameter("Usn");
String password = request.getParameter("Password");
out.println(username+" "+password);
}}
Servlet to UI (jsp file only)
itemlist.jsp (Create new jsp file in Project>WebContent>itemslist.jsp)
<html>
<body>
Servlet communicated message to JSP: ${jItem_list}
</body>
</html>
<body>
Servlet communicated message to JSP: ${jItem_list}
</body>
</html>
Serverlet.java
request.setAttribute("jItem_list", json);
rd = request.getRequestDispatcher("/itemslist.jsp");
rd.forward(request, response);
rd = request.getRequestDispatcher("/itemslist.jsp");
rd.forward(request, response);
No comments:
Post a Comment