I have a Java EE website in process, but I still need to manage the login and registration .
I would like you to give me a guide on how to do it using Servlets. For example, have a .jsp registro.jsp
send via a <form></form>
to Servlet
a RegistroServlet
. I already have that part of registration almost mastered, BUT I would like to manage the session. If it is administrador
, let the system detect it for me, and if it is a usuario registrado
too. So depending on what type of profile you have, I am redirected to the corresponding page
I don't know if the information is useful, but I have a database with a table usuario
and another TipoUsuario
with a @ManyToOne relationship. I created it so that if a user is created it needs the user type as well. How do I go about handling that session? I'm new to Java EE, I've been watching videos but none of them manage to teach me what I need, I appreciate any help in advance.
PS: already in the table TipoUsuario
I have two fields: User and Administrator
EDIT
userRecord.jsp
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link type="text/css" rel="stylesheet" href="../css/estilo.css">
<title>Predicación pública - Registro</title>
</head>
<body>
<br>
<br>
<br>
<div class="register_form">
<center>
<fieldset>
<legend align="center"><h1><strong>¡Regístrate!</strong></h1></legend>
<form action="" method="POST">
<label for="nombre"><strong>Nombre: <div class="asterisco">*</div></strong></label><br>
<input type="text" name="nombre" required> <br>
<label for="apeliido"><strong>Apellido: <div class="asterisco">*</div></strong></label><br>
<input type="text" name="apellido" required><br>
<label for="usuario"><strong>Usuario: <div class="asterisco">*</div></strong></label><br>
<input type="text" name="usuario" required><br>
<label for="contrasena1"><strong>Elija su contraseña: <div class="asterisco">*</div></strong></label><br>
<input type="password" name="contrasena1" required><br>
<br>
<input type="submit" name="enviar" value="Aceptar">
</form>
</fieldset>
</center>
</div>
</body>
ServletUserRegistry.java
package com.carrito.servlets;
import com.carrito.model.TipoUsuario;
import com.carrito.model.Usuario;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class RegistroPublicadorServlet extends HttpServlet {
/**
* Processes requests for both HTTP <code>GET</code> and <code>POST</code>
* methods.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
String action= request.getServletPath();
switch(action){
case "/registroPublicador.":
{
String nombre = request.getParameter("nombre").trim();
String apellido = request.getParameter("apellido").trim();
String password = request.getParameter("passowrd");
//implementación del método guardar
Usuario usuario = new Usuario();
usuario.setNombreUsuario(nombre);
usuario.setContraseñaUsuario(password);
TipoUsuario perfil = new TipoUsuario();
perfil.setNombrePerfil("Administrador");
usuario.setTipoUsuarioidTipoUsuario(perfil);
To get/login the user, just call the object
HttpSession
from the request:Hint: You seem to be working with Netbeans, which creates a method
processRequest
for both thedoGet
and thedoPost
. This is bad practice and you should not use it. ThedoGet
is used to pre-process the view, while thedoPost
is used to process the action. I recommend you act directly on each method.If you are using JavaEE you might be more inclined to use JSF which already has mature session handling and according to the java enterprise standard is the official web tier implementation.
Java enterprise 7 jsf 6 official information
Information java enterprise edition 7 jsf
If you use jsf then you can use an object called FacesContext which is the one that handles everything related to session as follows