Commit d4b588da by OscarGonzalez97

ejemplo de requestDispatcher

parent 1dd26d2a
package com.roshka.bootcamp;
import jakarta.servlet.RequestDispatcher;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
@WebServlet("/login")
public class RequestDispatcherServlet extends HttpServlet {
@Override
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
PrintWriter out = response.getWriter();
String username = request.getParameter("username");
String password = request.getParameter("password");
if (username.equals("bootcamp") && password.equals("bootcamp")) {
// forward
RequestDispatcher dispatcher = request.getRequestDispatcher("RequestDispatcherServletSuccess");
dispatcher.forward(request, response);
} else {
//include
out.print("Usuario invalido!");
RequestDispatcher rd = request.getRequestDispatcher("/login.html");
rd.include(request, response);
}
}
}
package com.roshka.bootcamp;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
@WebServlet("/dispatcher")
public class RequestDispatcherServletSuccess extends HttpServlet {
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
String n = request.getParameter("username");
out.print("Welcome " + n);
}
}
body {
background-color:powderblue;
}
\ No newline at end of file
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Login</title>
<link rel="stylesheet" href="estilos/login.css"></link>
</head>
<body>
<form method="post" action="login">
<label>username: </label>
<input type="text" name="username"><br/>
<input type="password" name="password"><br/>
<input type="submit">
</form>
</body>
</html>
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment