0

i am new here and not native english speaker :). So, i programmed a form that sent simple values via a post method within a java servlet. At first, it worked with request.getParameter(), i don't know what i did, but it doesn't work anymore.

package servlets;

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;

//import services.*;

//import org.json.JSONException;
//import org.json.JSONObject;

public class Authentification extends HttpServlet{

    public void doGet(HttpServletRequest req, HttpServletResponse res) throws  ServletException, IOException {
        res.setContentType("text/html");
        PrintWriter writer = res.getWriter();

        writer.println("<h1>Bienvenue sur notre nouveau site WEB</h1>");
        writer.println("<body>");

        //Form
        writer.println("<form action="+"Authentification"+" method="+"post"+" class="+"form"+">");

        //login
        writer.println("<div class="+">");
        writer.println("<label for="+"login"+">Login:");
        writer.println("<input type="+"text"+" name="+"login"+"/>");
        writer.println("</label>");
        writer.println("</div>");

        //password
        writer.println("<div class="+">");
        writer.println("<label for="+"pwd"+">Password:");
        writer.println("<input type="+"text"+" name="+"pwd"+"/>");
        writer.println("</label>");
        writer.println("</div>");

        //button form
        writer.println("<div class="+"button"+">");
        writer.println("<button type="+"submit"+" name="+"button_connexion"+" value="+"Se_connecter"+">Connexion</button>");
        writer.println("</div>");
        writer.println("</form>");

        //aller sur le lien de l'inscription
        writer.println("<a href="+"/ProjetWeb2020/Inscription"+">T'es nouveau?Par ici l'inscription</a>");
        writer.println("</body>");


    }

    public void doPost(HttpServletRequest req, HttpServletResponse res) throws  ServletException, IOException {
        res.setContentType("text/html");
        PrintWriter writer = res.getWriter();

        String login, pwd;

        login=req.getParameter("Login");
        pwd=req.getParameter("Pwd");

        if(login==null && pwd==null){
            writer.println("<h1>Not good!</h1>");
        }
        //JSONObject obj=services.Authentification.loginUtilisateur(login, pwd);

        writer.println("<h2>login is:"+login+"</h2>");
        writer.println("<h2>pwd is:"+pwd+"</h2>");
    }
}

And the result i don't want no more.

enter image description here

I will aprecciate any answer with great pleasure :)

4
  • 1
    You have typo in your code change login=req.getParameter("Login"); to login=req.getParameter("login"); and pwd=req.getParameter("Pwd"); to pwd=req.getParameter("pwd"); Commented Apr 8, 2020 at 16:59
  • Thank you very much for your quick answer :) but the issue is not about the typo, i noticed it to kumar also. Commented Apr 8, 2020 at 18:33
  • check your browser console->inspect-> html (See if correct html is generated also inputs name are same or not ) Commented Apr 9, 2020 at 9:49
  • I looked at the html code and here it is: <h1>Bienvenue sur notre nouveau site WEB</h1> <body> <form action=Authentification method=post class=form> <div class=> <label for=login>Login: <input type=text name=login/> </label> </div> <div class=> <label for=pwd>Password: <input type=text name=pwd/> </label> </div> <div class=button> <button type=submit name=button_connexion value=Se_connecter>Connexion</button> </div> </form> <a href=/ProjetWeb2020/Inscription>T'es nouveau?Par ici l'inscription</a> </body> Commented Apr 9, 2020 at 13:33

2 Answers 2

1

So, thank you everybody, and especially Swati for the solution. Yes, the issue as a typo thing about "backslash".

I give you the correct code:

package servlets;

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;

//import services.*;

//import org.json.JSONException;
//import org.json.JSONObject;

public class Authentification extends HttpServlet{

    public void doGet(HttpServletRequest req, HttpServletResponse res) throws  ServletException, IOException {
        res.setContentType("text/html");
        PrintWriter writer = res.getWriter();

        writer.println("<h1>Bienvenue sur notre nouveau site WEB</h1>");
        writer.println("<body>");

        //Form
        writer.println("<form action="+"\"Authentification\""+" method="+"\"post\""+" class="+"\"form\""+">");

        //login
        writer.println("<div class=\" \">");
        writer.println("<label for="+"\"login\""+">Login:");
        writer.println("<input type="+"\"text\""+" name="+"\"login\""+"/>");
        writer.println("</label>");
        writer.println("</div>");

        //password
        writer.println("<div class=\" \">");
        writer.println("<label for="+"\"pwd\""+">Password:");
        writer.println("<input type="+"\"text\""+" name="+"\"pwd\""+"/>");
        writer.println("</label>");
        writer.println("</div>");

        //button form
        writer.println("<div class="+"\"button\""+">");
        writer.println("<button type="+"\"submit\""+" name="+"\"button_connexion\""+" value="+"\"Se_connecter\""+">Connexion</button>");
        writer.println("</div>");
        writer.println("</form>");

        //aller sur le lien de l'inscription
        writer.println("<a href="+"/ProjetWeb2020/Inscription"+">T'es nouveau?Par ici l'inscription</a>");
        writer.println("</body>");


    }

    public void doPost(HttpServletRequest req, HttpServletResponse res) throws  ServletException, IOException {
        res.setContentType("text/html");
        PrintWriter writer = res.getWriter();

        String login, pwd;

        login=req.getParameter("login");
        pwd=req.getParameter("pwd");

        if(login==null && pwd==null){
            writer.println("<h1>Not good!</h1>");
        }
        //JSONObject obj=services.Authentification.loginUtilisateur(login, pwd);

        writer.println("<h2>login is:"+login+"</h2>");
        writer.println("<h2>pwd is:"+pwd+"</h2>");
    }
}

And the syntax of the html page:(you can check the ancient html code in my previous comments)

<h1>Bienvenue sur notre nouveau site WEB</h1>
<body>
<form action="Authentification" method="post" class="form">
<div class=" ">
<label for="login">Login:
<input type="text" name="login"/>
</label>
</div>
<div class=" ">
<label for="pwd">Password:
<input type="text" name="pwd"/>
</label>
</div>
<div class="button">
<button type="submit" name="button_connexion" value="Se_connecter">Connexion</button>
</div>
</form>
<a href=/ProjetWeb2020/Inscription>T'es nouveau?Par ici l'inscription</a>
</body>

Moral of the story:be careful with " ", use Backslash in strings for html code!

Sign up to request clarification or add additional context in comments.

Comments

0

change the param names while accessing. login=req.getParameter("login"); pwd=req.getParameter("pwd");

it will work.

1 Comment

Thank you very much for your quick answer :) I modified the name attribute of each input and didn't modify in the getParameters() to fit the names. But the issue not there, it doesn't work.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.