1. Servlet Mapping in web.xml
Transfering control from JSP (Login.jsp) file to a servlet (RemController.java) file.
----------------------------------------------------------------------------------------------------------------------------
Login.jsp
<form action="rem" method="POST">
Username: <input type="text" id="username" name="username" value=""/><br/>
Password: <input type="password" id="password" name="password" value=""/><br/>
<input type="submit" id="submit" value="Submit" /><br/>
</form>
----------------------------------------------------------------------------------------------------------------------------
web.xml
<servlet>
<servlet-name>RemController</servlet-name>
<servlet-class>myPkg.RemController</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>RemController</servlet-name>
<url-pattern>/rem</url-pattern>
</servlet-mapping>
----------------------------------------------------------------------------------------------------------------------------
RemController.java
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setAttribute("result", "hello!");
String name = request.getParameter("username");
String password = request.getParameter("password");
System.out.println(name+" "+password);
// response.sendRedirect("Remember.jsp");
request.getRequestDispatcher("Remember.jsp").forward(request, response);
}
----------------------------------------------------------------------------------------------------------------------------
Remember.jsp
<html>
<body>
<h1>Remember.jsp</h1>
<p>The result is ${result}</p>
</body>
</html>
----------------------------------------------------------------------------------------------------------------------------
Transfering control from JSP (Login.jsp) file to a servlet (RemController.java) file.
----------------------------------------------------------------------------------------------------------------------------
Login.jsp
<form action="rem" method="POST">
Username: <input type="text" id="username" name="username" value=""/><br/>
Password: <input type="password" id="password" name="password" value=""/><br/>
<input type="submit" id="submit" value="Submit" /><br/>
</form>
----------------------------------------------------------------------------------------------------------------------------
web.xml
<servlet>
<servlet-name>RemController</servlet-name>
<servlet-class>myPkg.RemController</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>RemController</servlet-name>
<url-pattern>/rem</url-pattern>
</servlet-mapping>
----------------------------------------------------------------------------------------------------------------------------
RemController.java
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setAttribute("result", "hello!");
String name = request.getParameter("username");
String password = request.getParameter("password");
System.out.println(name+" "+password);
// response.sendRedirect("Remember.jsp");
request.getRequestDispatcher("Remember.jsp").forward(request, response);
}
----------------------------------------------------------------------------------------------------------------------------
Remember.jsp
<html>
<body>
<h1>Remember.jsp</h1>
<p>The result is ${result}</p>
</body>
</html>
----------------------------------------------------------------------------------------------------------------------------
No comments:
Post a Comment