0

Passing Parameter to the Java file using Html form I couldn't run in my Tomcat server. Who can describe this example step by step? where shall I put .html file and where .java and . class files. I am attaching html and java file. The compiled file with .class extension I have already done! Thank you in advance!

<html>

<head>
<title>New Page 1</title>
</head>

<body>

<h2>Login</h2>
<p>Please enter your username and password</p>
<form method="GET" action="/htmlform/LoginServlet">
  <p> Username  <input type="text" name="username" size="20"></p>
  <p> Password  <input type="text" name="password" size="20"></p>
  <p><input type="submit" value="Submit" name="B1"></p>
</form>
<p>&nbsp;</p>

</body>

</html>

and this is my LoginServlet.java file:

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class LoginServlet extends HttpServlet{
  public void doGet(HttpServletRequest request, HttpServletResponse response)
                                   throws ServletException, IOException {
    response.setContentType("text/html");
    PrintWriter out = response.getWriter();
    String name = request.getParameter("username");
    String pass = request.getParameter("password");
    out.println("<html>");
    out.println("<body>");
    out.println("Thanks  Mr." + "  " + name + "  " + "for visiting roseindia<br>" );
    out.println("Now you can see your password : " + "  " + pass + "<br>");
    out.println("</body></html>");
  }
}

2 Answers 2

4

The HTML file has to go in public webcontent and the class file has to go in /WEB-INF/classes in the right folder structure which conforms the class' package declaration. Also don't forget to declare the servlet in /WEB-INF/web.xml.

As to the HTML form, you need to realize that a leading slash / in the URL will bring you to domain root. So effectively your form submits to http://localhost:8080/htmlform/LoginServlet. You need to ensure that the webapp context path is indeed named /htmlform and that the Servlet is in web.xml been mapped on an url-pattern of /LoginServlet.

See also:

  • About Servlets - Contains a Hello World example and several useful tutorial/resource links.

Unrelated to the problem, Roseindia.net is considered the worst learning/example source. Relatively a lot of snippets over there are cluttered by bad practices. I'd suggest to ignore that list as long as you're new.

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

7 Comments

Thank You for the fast reply! Actually I have put .java file in /WEB-INF/classes, and also set the web.xml . I am sorry, but what did you mean, when said "public webcontent"??
The .class file, not the .java file. The public webcontent is there where your /WEB-INF folder is been placed. Did you click and follow the "Abous Servlets" link anyway?
Thank you very much, I am going to try now! and thank you about roseindia :)
I followed About Servlets link. I am having some problem there. I did all as described, but exception report was shown in browser. I set web.xml, put .jsp file in webapps\contextname\WEB-INF, put the HelloServlet.class in classes/com/example folder. web.xml is also in WEB-INFO folder. And finally in browser i paste the path localhost:8080/contextname/hello
What exception exactly did you get? Just a "Resource not found"?
|
0

I'm not certain I fully understand the question but it seems to be asking where to put java code and html files for tomcat to use them. These resources are typically packaged together into a .war file and deployed into tomcat.

4 Comments

The question wasn't asked very good, sorry for my english! I have html and java files, I have set my web.xml. Now I running my Tomcat server,but don't know how can I send data from html to java. LoginServlet.java and LoginServlet.class are in 'classes' folder. web.xml is in WEB-INF folder. But where to put html file,? in which folder? and also the problem is that i can't manage the paths, I mean for example in html file arethe string like this : "action="/htmlform/LoginServlet", from where does this path's start begin? from htmlform folder or ...?
Typically, yes. Necessary, no.
I can't find out the problem. After clicking on submit button, page goes to file://localhost/LoginServlet?username=sdhsdghgh&password=ghghgghgh&B1=Submit, and couldn't open the file. What am I doing wrong? help me to find out my problem please
I have noticed this: if I am clicking on Submit button, the url is written like file://localhost/htmlform/LoginServlet?username=Vardges&password=Begoy&B1=Submit, but if I am correcting the url path from the browser like localhost:8080/htmlform/…, it is working. Can anybody explain how and from where can I set the url path before clickin on submit button. Thank you in advance!

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.