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".

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.