Articles tagged with: java servlet
Java Codes »
A servlet can use getRemoteAddr() and getRemoteHost() to retrieve the IP Address and host of the client machine respectively:
public String ServletRequest.getRemoteAddr()
public Stirng ServletRequest.getRemoteHost()
Using these functions access to specific client host can be restricted. The sample code below checks the client specification at the server and send the relevant message to the client.
import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class DemoExportRestriction extends HttpServlet{
public void doGet(HttpServletRequest req,HttpServletResponse res)
throws ServletException,IOException{
res.setContentType(“text/plain”);
PrintWriter out= res.getWriter();
//Getting the client’s hostname
String remoteHost = req.getRemoteHost();
//See if the client is allowed
if(!isHostAllowed(remoteHost)){
out.println(“Access <BLINK>ACCESS DENIED </BLINK>”);
} else{
out.println(“access granted”);
}
}
private boolean isHostAllowed(String host) {
return(host.endsWith(“.com”))||
(host.indexOf(‘.’)==-1);//no domain , assume OK
}
}
Java Codes »
Servers like JRun and other provide separate logs where all your print requests print their data. For example requests to System.out. And System.err goes to different log files. If you want to maintain logs separately for each module then you can create a static class called “LogWriter” and call the method inside the try catch loop as follows.
try
{
// write your code here
}
catch(Exception e)
{
LogWriter.print(“Debug : error occurred at function ”)
}
Java Codes »
For example, you have two servlets (Servlet1 and Servlet2).
In Servlet1, you have a method called getString().
In Servlet2, use
ServletContext context = getServletContext();
to get servlet context of Servlet2. Then use
Servlet1 servlet = (Servlet1)context.getServlet(“Servlet1″);
to get the Servlet1 servlet object.
Now, you can Call Servlet1 method from Servlet2 with
servlet.getString();
Java Codes »
This J2EE tip demostrates chaining method in servlets. Servlet Chaining means the output of one servlet act as a input to another servlet. Servlet Aliasing allows us to invoke more than one servlet in sequence when the URL is opened with a common servlet alias. The output from first Servlet is sent as input to other Servlet and so on. The Output from the last Servlet is sent back to the browser. The entire process is called Servlet Chaining.
// FirstServlet
import javax.servlet.*;
import java.io.*;
import javax.servlet.http.*;
public class FirstServlet extends HttpServlet {
String name ;
ServletConfig config;
…
Java Codes »
This J2EE tip demonstrates the difference in use of between encodeURL and sendRiderct. Further it also demonstrates the neccesiaty for encodeURL. In order for the URL’s to be in universal specificied format it must be specified in the return type of encodeURL which is then passed on to the sendRedirect.
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
public class EncodeURL extends HttpServlet {
public void doGet(HttpServletRequest request ,
HttpServletResponse response)
throws ServletException , IOException {
response.setContentType(“text/plain”);
PrintWriter out = response.getWriter();
String encode = response.encodeURL(“http://www.google.com”);
response.sendRedirect(encode);
}
public void doPost(HttpServletRequest request,
HttpServletResponse response)
throws ServletException , IOException {
doGet(request , response);
}
}
Java Tutorials »
(Part1) – (Part2) - (Part3) – Part4
Implementation of Java Servlet
Back to our Eclipse and modify your index.jsp to something like below.
<form action=”GreetingServlet” method=”POST”>
First Name: <input type=”text” name=”firstName” size=”20″><br>
Surname: <input type=”text” name=”surname” size=”20″>
<br><br>
<input type=”submit” value=”Submit”>
</form>
It means that every time we execute the Submit button in our JSP, it will call GreetingServlet that we created earlier. However, now, our Java Servlet actually do nothing. We need to modify our GreetingServlet.java as well.
This GreetingServlet.java contains our implementation of the Java Servlet. If you carefully pay attention to this file, you …
Java Tutorials »
(Part1) – (Part2) – Part3 – (Part4)
How to Start Tomcat Manually
If you have successfully configured Tomcat, you should have a bin folder of Tomcat. Remember that you need to download the Tomcat from Apache website in the form of zip file. Do not download the .exe file as it would be good if you have a manual hands-on experience on this. It should help you to understand how Tomcat works.
I have extracted my Tomcat into C:\tomcat-5.5.16. Thus, I should have a bin folder that …
Java Tutorials »
(Part1) – Part2 - (Part3) – (Part4)
Implementation of Tutorial’s Example
For our tutorial, we are going to use index.jsp to demonstrate how to implement Java Servlet that greets the users. Index.jsp will be used to obtain the first name as well as the last name / surname of the users. For this case, textboxes should be adequate as first name and surname. However, it would be different case if we are required to get the country of origin of the users. The use of combobox would be more appropriate for choosing …
Headline, Java Tutorials »
Part1 – (Part2) - (Part3) – (Part4)
Introduction
Java Servlet is the one of the most important Java technologies. It is the simplest model to build a complete Java J2EE Web Application. Furthermore, even for complex J2EE Web Application that uses Struts, Spring, EJB and etc, they are still using Servlet for certain purposes such as Servlet Filter, Listener and etc. Thus, it is just a good idea for you to have well-built understanding of Java Servlet. Prior reading this tutorial, it would be excellent if you have mastered the basic …
