-2

So I have this piece of code in my .jsp file

<table border="0" style="width:400px;">
    <form class="Credentials" action="SellerServlet" method="post">
        <tr style="height: 40px">method
            #User name
            <td style="width: 20%; text-align: right;">
                <label for="Name"><b>Name:</b></label>
            </td>
            <td>
                <input type="text" id="Name" name="Name" required>
            </td>
        </tr>

        #Surname
        <tr style="height: 40px">
            <td style="width: 20%; text-align: right;" >
                <label for="surname"><b>Surname:</b></label>
            </td>
            <td>
                <input type="text" id="Surname" name="Surname" required>
            </td>
        </tr>

        #Username
        <tr style="height: 40px">
            <td style="width: 20%; text-align: right;">
                <label for="username"><b>Username:</b></label>
            </td>
            <td>
                <input  type="text" id="username" name="user" required>
            </td>
        </tr>
        <tr style="height: 40px">
            <td style="width: 20%; text-align: right;" >
                <label for="password"><b>Password:</b></label>
            </td>
            <td>
                <input type="password" id="password" name="pass" required>
            </td>
        </tr>


        #User Type
        <tr style="height: 40px">
            <td style="width: 20%; text-align: right;" >
                <label for="type"><b>Type:</b></label>
            </td>
            <td>
                <input type="text" id="type" placeholder="Client" readonly>
            </td>
        </tr>

        #User Program
        <tr style="height: 40px">
            <td style="width: 20%; text-align: right;"  >
                <label for="program"><b>Program:</b></label>
            </td>
            <td>
                <select style="width: 150px" id="program" name="program" >
                    <option value="" selected disabled hidden>Επιλέξτε πρόγραμμα</option>
                    <option>asdfs</option>
                    <option>asdfas</option>
                    <option>asfdas</option>
                </select>
            </td>
        </tr>

        #Submit Button
        <tr style="height: 100px;">
            <td colspan="2" style="width: 50%">
                <center>
                    <button type="submit" name="Register" >Register</button>
                </center>
            </td>
        </tr>
    </form>
</table>

And my servlet code is

@WebServlet(name = "SellerServlet" , urlPatterns = {"/SellerServlet"})
public class SellerServlet extends HttpServlet {
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        String registerButton = request.getParameter("register");
        if(registerButton!=null){
            //request.getRequestDispatcher("/index.jsp").forward(request, response);
            String Username=request.getParameter("user");
            String Password=request.getParameter("pass");
            System.out.println(Username+Password);
            Seller CreateClient= new Seller();
            CreateClient.CreateClientAccount(Username,Password);
        }
    }
}

What the servlet here is supposed to do is pass the 2 values (user and pass) to an object of the class Seller to later do something with them, I didn't include that following part because it works it just gets the value of null from both and I have broken my head for 2 hours now trying to figure out why

In case needed: Using java 14 and tomcat 9

2 Answers2

0

in the jsp code you have

<input type="password" id="password" name="pwd" required>

so in the servlet code you have to respect the name in the tag, which is pwd

String Password=request.getParameter("pwd");

and for the button Register, you have to respect the cases. in jsp the name is "Register" with a capital R

String registerButton = request.getParameter("Register");

also try to add to a value to the button register

<button type="submit" name="Register" value="clicked">Register</button>
Bashir
  • 2,057
  • 5
  • 19
  • 44
  • I typed it wrong here that part is correct (post edited) – Anthony Kalampogias Jun 01 '20 at 22:00
  • I updated my answer as well, see the part about register – Bashir Jun 01 '20 at 22:00
  • Nice attention to detail but it is not relevant to my question my friend :) – Anthony Kalampogias Jun 01 '20 at 22:04
  • which variables are null? you'll never get inside the if block because the register button will always return null. also I don't see any need to test on the register button, because once you are in the servlet, it means you clicked on the button – Bashir Jun 01 '20 at 22:07
  • As i have written on the post it does get in the if,that's why I can see from the system.out that i get null and the object for my class is created – Anthony Kalampogias Jun 01 '20 at 22:28
  • I don't see any problem here, so weird. can you post all the code? may there is something else wrong (even if it works fine, please share all of it) I mean all the servlet code, all of it – Bashir Jun 01 '20 at 22:35
-1

What does this variable relate to?

String registerButton = request.getParameter("register");
  if(registerButton!=null){

If with

<button type="submit" name="Register" >Register</button>

The name is incorrect(Register vs register) and you do not need to check value of this button, just click the button submit and the form will do everything for you. you just need to get the passed arguments(ex, request.getParameter(user)). You don't get its value and just remove it from the code

Diss
  • 11
  • 2