Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JSP cant find the servlet reference (404)

So as I'm trying to run this code example https://www.youtube.com/watch?v=uNRSNDm08wk I did my servlet class and also the form is poiting at it. However when I clic submit it doesn't seem to recognize regServlet as the identifier for my servlet since it's giving me the error 404. It looks for a file inside the jsp page directory. Is there anywhere I can check for those references?

package bean;

import java.io.IOException;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class RegServlet
 */
@WebServlet("/RegServlet")
public class RegServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    StringBuilder csvSkills =   new  StringBuilder();

    /**
     * @see HttpServlet#HttpServlet()
     */
    public RegServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String hdnParam =   request.getParameter("pagename");
            if(hdnParam.equals("register")){
....

The html form:

<form action="RegServlet" method="post">
      <input type="hidden" name="pagename" value="login"/>
        <p>Nombre de Usuario</p>
        <input type="text" name="regUserName" required value="usuario" onBlur="if(this.value=='')this.value='usuario'" onFocus="if(this.value=='usuario')this.value='' "><br>
       <p>Contraseña</p>
        <input type="password" name="regUserPass"  required value="Contraseña" onBlur="if(this.value=='')this.value='Contraseña'" onFocus="if(this.value=='Contraseña')this.value='' "> 
        <br><input type="submit" value="Crear Cuenta">

      </form>
like image 776
Ale Rojas Avatar asked Oct 19 '22 15:10

Ale Rojas


1 Answers

I suggest you to add the context inside the action path dynamically in your JSP as:

<form action="${pageContext.request.contextPath}/sampleServlet">
like image 93
Viswanath Donthi Avatar answered Oct 29 '22 13:10

Viswanath Donthi