Ale Rojas
Ale Rojas

Reputation: 497

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>

Upvotes: 5

Views: 1153

Answers (2)

Tim Biegeleisen
Tim Biegeleisen

Reputation: 522581

As your code currently stands, your servlet should be accessible from the following path:

http://localhost:8080/{app-name}/RegServlet

where {app-name} is the name of the WAR file. If you cannot access this directly from a web browser, then likely the WAR does not exist, or the RegServlet.class file is not present inside the WAR.

When you try to access the servlet from your form, you need to make certain that it is using the above path or else it won't be accessible. You can check this from the JavaScript console.

Upvotes: 0

Viswanath Donthi
Viswanath Donthi

Reputation: 1821

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

<form action="${pageContext.request.contextPath}/sampleServlet">

Upvotes: 3

Related Questions