• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 197
  • Last Modified:

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 http://yahoo.com, 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.



 
0
pbhag
Asked:
pbhag
  • 11
  • 4
1 Solution
 
amitdCommented:
import java.net.*;
import java.io.*;

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
if(dis.readLine()==null)
{
 break;
}
}
}

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

<HTML><HEAD><TITLE>Login Page</TITLE>  </HEAD> <BODY>

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

</BODY></HTML>

0
 
shlomoyCommented:
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.
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
shlomoyCommented:
To learn about the servlet technology in Java check out:
http://java.sun.com/docs/books/tutorial/servlets/index.html
0
 
shlomoyCommented:
Now lets build MyServlet.java:

import java.io.*;
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";
      response.setContentType("text/html");
      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 = "http://www.experts-exchange.com";
      out.println("<P>");
      out.println("Hello, " + loginName + ".");
      out.println("The url for you is:");
      out.println(returnURL);
      out.println("</BODY></HTML>");
      out.close();
   }
}
0
 
shlomoyCommented:
You will need to download from java.sun.com
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').

Next,
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.
0
 
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
pbhag
0
 
shlomoyCommented:
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(returnURL);
to
out.println("<A HREF=" + returnURL + ">Click Here</A>");
0
 
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 ?

pbhag
0
 
shlomoyCommented:

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

0
 
pbhagAuthor Commented:
yes, exactly. so is it possible ?
its like the main Home URL and the other subsequest Urls.
Actually thats my question.
0
 
shlomoyCommented:
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.
0
 
shlomoyCommented:
Java has it's classes for parsing and handeling HTML:

 javax.swing.text.html
 javax.swing.text.html.parser
0
 
shlomoyCommented:
if you want the actual parser - use XML4J (ibm's XML parser - the best in Java) - to parse the HTML for you.
0

Featured Post

Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

  • 11
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now