HTTP sever in Java

I want to create a server side program in Java in which there r 2 requests and responses,
1. A client comes and gives its userid and password, Server validates it and returns his userid and a url addr (which stored somewhere in the database or a file system) to the user.
2.Now from this URL he can have access to number of other url locations, for eg, from say, he can land up in accessing any number of other URLs by following the links. Now i want this subsequent urls to be given back to the user. These subsequent links r also stored somewhere in the database or file system.
i need to know how do i go about writing the program without creating a client. Pls explain with the code.

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.


class Server
  private ServerSocket s;
  private Socket s1;
  public static void main(String args[])
s = new ServerSocket(4000);
 s1 = s.accept();

System.out.println("send name password");
DataInputStream dis = new DataInputStream(cli.getInputStream());
DataOutputStream dos = new DataOutputStream(cli.getOutputStream());
dis.readLine()//if u pass it on 1 line

this is just a example u can take it as a guide line
First, let's do the HTML page for client to access and input his login+password:


<FORM METHOD=POST ACTION="servlet/myServlet.class">
Login Name:
<INPUT NAME=login SIZE=20 align=right MAXLENGTH=20 WARP=VIRTUAL VALUE="login name here">
<INPUT NAME=password SIZE=20 align=right MAXLENGTH=20 WARP=VIRTUAL VALUE="password here">


next, we use the servlet technology to create the server side application which will read the login name and password, will verify them, get the url, and create the HTML page as a response to the client.
client will get the response to his browser.
Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

To learn about the servlet technology in Java check out:
Now lets build

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

public class MyServlet extends HttpServlet {
    * Handle the HTTP GET method by building a simple web page.
   public void doPost (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
      final String title = "Your URL";
      PrintWriter out = response.getWriter();
      out.println("<HTML><HEAD><TITLE>" + title + "</TITLE></HEAD><BODY>");
      out.println("<H1>" + title + "</H1>");
        String loginName = request.getParameter("login");
        String password  = request.getParameter("password");
// here do whatever you want with the login-name and the password
// I bet you'd like to verify them here and then look up a url in a database
// I'll assume I have that url already for this short snippet:
        String returnURL;
// here you will find the url and assign it to returnURL
// I'll just assign an arbitrary url, for this example:
        returnURL = "";
      out.println("Hello, " + loginName + ".");
      out.println("The url for you is:");
You will need to download from
the JSDK package in order to have the classes with the servlet technology

it's very easy to do.

Note the typing error I have in the HTML form:
instead of writing: myServlet.class write-
MyServlet.class (the 'm' is changed to 'M').

If you don't have a web server which can run servlets, use the jdsk server supplied in the java JSDK package.

This concludes my answer :-)

Good luck.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
pbhagAuthor Commented:
hey Shamoli,
thanks for the reply, but the thing is i dont want to craete it in Servlet, i want to use java application only.
And about the userid and password, they r already stored somewhere in a text file or something and so its easy to retrieve it.
But the thing i am looking for is, is it possible to open a URL connection in the browser thru that java application ?
I hope u got my point.
Thank u
Servlet is 100% pure java - so using servlet is, actually, using java.

yes it's possible.
plant a java-script whic does that on the HTML you build as a response.

also you can change the HTML you return to the client by planting the link to that url:

just change the line:
out.println("<A HREF=" + returnURL + ">Click Here</A>");
pbhagAuthor Commented:
hi shamoli,
well thanks for the reply. I will try out that thing and well yes, i know java servlet in a pure Java, but the thing was that i am not supposed to use Java servlet, since it is to be developed in simple application.

Ok, once u have got the URL working, how will u get the addresses of the subsequest URls ? meaning, when u follow a link, a new url is activated.
So can u tell me how is it possible to get the addr of such urls ?


you want the user to see the linksin that url, instead of the url itself?
pbhagAuthor Commented:
yes, the link addresses
so you actually want to extract all urls from a web page? is that it?

pbhagAuthor Commented:
yes, exactly. so is it possible ?
its like the main Home URL and the other subsequest Urls.
Actually thats my question.
I see now.
so, what you really need is an HTML parser.

you need to do two things:
1) get the HTML page on that url you want
2) parse that html page
3) use the parser to extract the links.
Java has it's classes for parsing and handeling HTML:

if you want the actual parser - use XML4J (ibm's XML parser - the best in Java) - to parse the HTML for you.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java EE

From novice to tech pro — start learning today.