Saturday, October 10, 2015

IP : Basic Form to Java Servlet Communication Tutorial


If you haven't tried the "Hello World" program in the blog, I recommend you to try it. Or else, you won't be able to follow this tutorial. And, you can use my code.

http://semlabs.blogspot.in/2015/09/ubuntu-java-servlet-code-for-displaying.html

because, I am gonna use the files in that and teach you stuff in this tutorial. Should have taught the Hello-World program itself actually, but I couldn't because of time constraints.

Anyways, let's start!

First of all, get the Hello-World Code files.

Now if you see, the folder is named "hello". This is the name of your web app.

A web application or web app is a client-server software application in which the client (or user interface) runs in a web browser. (Source : Wikipedia)

You would have placed the "hello" folder in your webapps folder, which is present in the tomcat files directory.

Now, let's take a look at the hierarchy of folders and files in "hello" folder.

cd /opt/tomcat/bin

./startup.sh



Below is a series of screen shots of the file explorer in my Ubuntu system.






Now, in each screen shot, if you see the top part, you will be able to see the path of the directories. That should help you understand what file is in which folder.

And, by the way, the "src" folder is not necessary, but we just keep it for our convenience.

But other folders : "WEB-INF" and "classes" are important and they must be named in the same way as they are named over here. These stuff are case sensitive, so please be careful when you are naming them, when you create a new web app.

So, now that we know the hierarchy of the folders, we are going to create a new web app with this hierarchy, and we will create the files one by one.

Go to your webapps folder. Create a new folder named "formexample".

Go inside this folder and create a new folder and name it "WEB-INF" and inside "WEB-INF" create two folders "classes" and "src", like how it was created in Hello-World program.

Now, we are going to create the files. First of all, let me tell you the aim of the program.

The aim is to display a form in a web page, which can contain anything, for simplicity, let's say we are going to get details of a student, we will just get his name. And when the student clicks "Submit" in the form, the form reaches the server and the server should display "Hi Karuppiah, your form is submitted" in a new web page, if the name typed was "Karuppiah".

First, let's create the web page with the form.

<html>

<head>
<title>Example</title>
</head>

<body>

    <form method="POST" action="server">
        Name : <input type="text" name="studname"> <br/><br/>
        <input type="submit" value="Submit">
    </form>
</body>

</html>

Now, that will create the form. And note the name attribute for the first input tag. The first input tag is used to create a text box to get text input, our name. And the next input tag is to create a button (meaning type is "submit") for submitting our form. Note the action attribute of form tag. We will talk about this soon.

And we are using POST method to submit information to the server. We can use GET method too. Google about the differences!

Place the example.html in "formexample" folder.

Now, let's create the server.java , the servlet code, which is used to send response webpage, when the user clicks Submit button in the form.

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

public class server extends HttpServlet
{
    public void doPost(HttpServletRequest request,HttpServletResponse response)
    {
        PrintWriter out = null;

        response.setContentType("text/html");
               
        try
        {
            out = response.getWriter();
           
            String studname = request.getParameter("studname");

            out.println("Hey " + studname);

            out.close();

        }
        catch(Exception e)
        {
            out.println(e);
        }

    }

}

The class's name is server. And since, we used POST method in our form, the method used is doPost(). It's actually a built in function of HttpServlet class, which we override for our purpose. And if we use GET method in our form, we override doGet().

The "request" object contains all the data sent by the client to the server and response is the object which is going to be sent by the server to the client as a response to the request. We use the request object to get the information typed by the client. Here, we use "studname" , the name attribute of the form, to refer to the  input tag in the form and to get the value typed in the text box in the form, by using getParameter() function. And to write to the response object, that is put data in the response object, we use PrintWriter class object "out". So, after getting the information typed by the client, we put this information along with the word "Hey " in the response object using "out" and now we are done. The servlet engine will take care of sending this response to the client who sent the request.

Place this server.java file in "src" folder, which is in formexample -> WEB-INF -> src

And compile this java program using the command

javac -cp /opt/tomcat/lib/servlet-api.jar server.java

If you installed your tomcat somewhere else, accordingly change the path in the command, to the appropriate path, instead of "/opt/tomcat" .

Place the class file in the "classes" folder, which is in
formexample -> WEB-INF -> classes

After this, we need to define the web.xml file

<web-app>

    <welcome-file-list>
        <welcome-file>example.html</welcome-file>
    </welcome-file-list>

    <servlet>
        <servlet-name>server</servlet-name>
        <servlet-class>server</servlet-class>
    </servlet>

    <servlet-mapping>
        <servlet-name>server</servlet-name>
        <url-pattern>/server</url-pattern>
    </servlet-mapping>
</web-app>

"welcome-file" tag is used to mention the first page or welcome page of the webapp. Others are self explanatory. "servlet-name" is the name of servlet, you can give anything. "servlet-class" is the name of the class file which we get after compiling the servlet's java file. Here we created server.java, so when we compile, we will get server.class , so the class name is "server".

And coming to "url-pattern" , well, this will tell the url to be displayed when the response page of the server is displayed. And remember to put the forward slash along with the name you want in the url. For me it's "/server". And the name you mention here in the "url-pattern", by name, I mean "server" in my case, excluding the slash; this is the same name as the one provided in the action attribute in the form tag! If the url pattern name and the action tag's attribute are not the same, you are gonna have issues!

And after creating this web.xml, place it in "WEB-INF" folder, which is in
formexample -> WEB-INF

Now, go to the tomcat's bin folder and execute "startup.sh"

cd /opt/tomcat/bin

./startup.sh

And then, go to your browser and type

localhost:8080/formexample

And then type a name and press Enter !

Below are some screen shots. Note the URLs in the images!

Below are the source files !












Tuesday, October 6, 2015

Tips For IP Lab ?

[ This is the same stuff that I sent for IP Model Lab, through mail. If you haven't seen the mail, see this ]

** Note : BEWARE. It's a long post! Never thought I would type so much! **

Our IP Semester Practicals is gonna come soon. It's high time you started coding, if you haven't.
And it's not all about just knowing how to code.

"What else do I need to know?" - you need to know the possible errors while you code. And it's not very easy this time, if I have to tell the truth.

There will be a hell lot of possible problems/errors, when it comes to Servlets and JSP stuff. And I think it will be more, when it comes to XML, I don't know much, since I am yet to learn it. And, don't think the compilers will help you. Sometimes, there are database problems, which don't appear in the webpage, unless you use some good error catching and error printing techniques, which I will talk about later. And then other times, even when compilers help you...it's not enough help. It's really tough to understand what's the actual problem.

Besides all this, there's our crappy lab. Sometimes there are missing files. Sometimes, you have to do some manual stuff to compile things. Like, in servlets, there's this servlet-api.jar file that you need when you compile the servlet program. So, if it's not available along with your java library files, well, you will have to use an option called "classpath" while compiling, and use the servlet-api.jar present in the lib folder of the apache-tomcat files. If you have already coded some servlet programs, especially, in your own pc, you would definitely know all this, because sometimes, that servlet-api.jar is present in some of the lab computers and we never know what we did in lab.

That's just one thing, that I talked about. There's one more important jar file for servlet programs...and even JSP programs. It's mysql-connector.jar , do google about it. I know very little about it. It provides standards-based drivers for JDBC, ODBC etc to use MySQL in any application (Googled it). And then, there are some jar files to use mail related functions, in the surgemail exercise, i suppose. These are some problems of missing files. I hope the lab is ready without such problems.

And then there are some technical/logical problems that I faced. Like, there was a problem, where my webpage would never open in the browser. All my files were indeed right. The problem was that, my server was not running properly even though I had started it using startup.sh ; Meaning - even the tomcat homepage wouldn't open in that case. But I was going straight to my own homepage, I mean, my webapp's homepage, so I never realized this problem. Later, I found out that I didn't shutdown the server and simply ran the startup.sh twice or thrice. Sometimes it's a problem. I m not sure about the intricate details. So, better startup only once. And if you changed your files (even html files) , do shutdown once and startup once. Only once. Besides, if you try to use shutdown.sh twice in a row, it would give you an error saying tomcat might already be shutdown and might not be running. That's not the case with startup.sh ; So, be careful. And always open the tomcat homepage first, check if it's working, then go to your webapp's page.

Then there's this crazy problem with cached pages. Sometimes, even when you change your html page. and even when you reload, the browser sometimes loads the same old page, using it's cache, instead of the new page. Caching is usually done for optimization, but this might be a problem for us, of course. So, if you change html files, do check the source html file by right clicking on the page and selecting "View Page Source". And this is also a good habit to check if your html page is rendered properly.

Besides all this, there are some HTTP Response Codes that you need to learn.
Especially, the Error Code, which are like 4xx and 5xx, some 3 digit numbers starting with 4 and 5.
4xx means client error : that is, the client is doing something wrong. Probably, you are requesting for some wrong resource and it would say "Error 404 : File or resource not found". That's a very common error. This way, you can realize there's something wrong you are doing on your side, like there's 5xx error, where there's problem in the server side. But yeah, 4xx error is not that helpful at all times though. If you want to know what are the possible causes, you could mail me and I will tell you whatever problems I have encountered till now. One of them, is the worst. It was a problem in my web.xml and it took me so much time to realize it. Even a small "/" closing tag slash can be a problem. I missed it for the closing tag of url-pattern tag. And my webpage kept saying 404 File Not Found. But tomcat homepage opened. That should mean how important the web.xml file is. Let it be servlets or JSP. But yeah, in JSP, just telling about the welcome file in the web.xml is enough I suppose. All the jsp-file tags is not necessary. Anyways, it's all your wish. And then, I encountered this another error, which is error 405 : Method Not Allowed. You should google that out and find about it.
5xx means server error : that is, there is some problem in the server side. Like, some compilation problem occurred, after converting a JSP file to a servlet java file and trying to compile it. There will be errors like ClassNotFoundException and stuff like that, like how it is displayed while compiling. I encountered a lot of 5xx errors while doing JSP programs. But this time it's good, because these errors are shown in the page. These are compiler errors mostly. So, you can understand them to some extent. There was another problem that I encountered while trying JSP programs. I believe it was some ClassNotFoundException...later I tried to start the tomcat server as root, that is run startup.sh as root, and my page worked fine. I am yet to find out more about it.
So that's some good list of possible errors...I am not trying to scare you. I m just telling you that you should start concentrating on these too.
It's not bad to make mistakes.... have some errors in your code and stuff. But you should know what probably caused it and what might be the possible solution. You should be smart enough to debug it. Or you cannot afford to make mistakes. So, start coding. And code from scratch. Like, really from scratch. So, that, you can know what mistakes you do. And to know what are the possible errors. Like, seriously. And understand stuff at least at an abstract level, to start with.
If you still don't understand the seriousness, you can read about an incident (see below) that happened to me.

Recently, I was trying to code a Servlet program in lab. The student information system. I coded the core of the program. Like, the basic stuff for a servlet and compiled and stuff and opened up the webpage to see how it works and I got some big errors saying 'can't access members with modifiers "" ' as far as i remember. I was actually using some variables in my servlet class, to use it in the different functions that I have. I never declared if they are private or public or what. So, I thought that was the mistake. And tried changing them, but nothing helped. Later, when I googled and check StackOverflow, they said I have to declare my Servlet class as public like :

public class server extends HttpServlet{


}

like that. So, I missed the public keyword. Well, we usually don't use it when we write programs. We just write "class employee { }" and similar stuff. Anyways, I changed that and it worked fine. I didn't look much into the error though. Like, why it was a problem. It was saying something about not being able to call or access the server class, I suppose. Do google if you are curious and if you find the problem, please do share it with me.

So, that's an example of how crazy mistakes can happen even because of a single keyword like "public".

Saturday, October 3, 2015

Computer Graphics : 3D using glut Tutorial




Thanks to whoever mentioned that comment (in cplusplus.com ) about this super cool site. It's called

www.videotutorialsrock.com

I have just started to check out the material and the videos. Looks like they will help a lot! Do check it! :D It's only for 3D though. And I think we need more knowledge about 3D, as of now...because we are well versed in 2D.

And there's one more comment



Check this site too! :)

www.zeuscmd.com/tutorials/opengl/index.php