0

Okay, I have no idea about what I am doing exactly but I am trying to understand it through sample programs and snippets. To be clear, my concepts are missing. So I might be having an error which is very obvious (not to me).

I am creating a simple username-password field on a localhost and need to be able to use the textfield inputs to match them. My page shows the buttons and fields properly(index.htm in WEB-INF) but my java class needs to get the string values. I get it that form actions need URL but how can I send it to my java clasS? I tried using request.getParamater("id") but it says no attached doc.

below is the html part

<!DOCTYPE html>
<html>

<head><title> Multi Factor Authentication Prototype </title>
<link rel="stylesheet"
  href="./css/styles.css"
  type="text/css"/>
</head>


<body>
<table class="title">
  <tr><th>Multi-factor Authentication, level 1 prototype </th></tr>
</table>

<p/>
<fieldset>
<form name="mfa1" action="check" method="post">
<legend> Login </legend> <ul> <br />
Username : <input title="Please Enter the Username" id="uname" name="uname" type="text" size="20" /><br />
<br />
Password : <input title="Please Enter your password" id="pwd" name="pwd" type="password" size="20" /><br />
<br />

</body>
</html>

here is java class. It says no java doc referred in request field and yes, doesn't display anything.

package test;

import java.io.*;

import javax.servlet.*;
import javax.servlet.annotation.*;
import javax.servlet.http.*;

import java.lang.*;

@WebServlet("/check")

public class TestServlet extends HttpServlet{


@Override
  public void doGet(HttpServletRequest request,
                    HttpServletResponse response)
      throws ServletException, IOException {
    response.setContentType("text/html");
    String un = request.getParameter("uname");
    PrintWriter out = response.getWriter();
    out.println
      ("<!DOCTYPE html>\n" +
       "<html>\n" +
       "<head><title>tuiy</title></head>\n" +
       "<body bgcolor=\"#fdf5e6\">\n" +
       "<h1>"+un+"</h1>\n" +
       "\n" +
       "</body></html>");


}
}

or is there a way to insert if/else clauses and various functions in the same dcument? I would really prefer if you could help me point out the way to get the parameters in java.

1 Answer 1

3

You are using a doGet method, but your form is using POST. Use a doPost method. See also doGet and doPost in Servlets

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

3 Comments

it is working but what is the main difference is what I mean to ask
Oh, just to make sure I udnerstood your link correctly, if I make it a get then changes in the url can lead to the page which otherwise should be acceptable only after authentication. Am I right?
Well, GET and POST are just different ways of transmitting information between the Browser and your application. Usually you use GET for accessing information from the server (hence it's named GET) and POST for modifying information or sending things like login data to your app (hence it's named POST). That of course is just a recommendation and you'll find many examples where it's done otherwise (whether or not this is a good idea is another discussion). So a short rule of thumb: if it's a read (like a search form) use GET, if it's a write/login use POST.

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.