| | Can anyone post here one or some examples of Simple Java Servlets please? I need more resources to do my assignments. Searching the web takes up too much time. So I am using one of the purpose of a forum, sharing ideas....lol |
|
|
Hi;
to show just how easy this is, here is probably the simplest/shortest servlet you can write: import java.io.*; import javax.servlet.*; import javax.servlet.http.*; public class HelloWorld extends HttpServlet{ public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException{ res.setContentType("text/html"); PrintWriter out = res.getWriter(); out.println("Hello World"); } } I suggest if/when you get the time to go through sun's course: http://java.sun.com/developer/onlineTraini...s/Fundamentals/ there's obvioulsy so much more you can do with servlets, so you need to be more precise asto what you need to know/achieve. It may even be the case that you should be looking at JSP instead of servlets... hope this helps - arp
good advice!
But have you got a servlet container for your code? and do you know how to deploy the code?
very simple servlet:
//////////////////////////////////////////////// import javax.servlet.*; import javax.servlet.http.*; import java.io.*; public class SimpleServlet extends HttpServlet { int i = 0; // Servlet "persistence" public void service(HttpServletRequest req, HttpServletResponse res) throws IOException { res.setContentType("text/html"); PrintWriter out = res.getWriter(); out.print("<HEAD><TITLE>"); out.print("simple servlet"); out.print("</TITLE></HEAD><BODY>"); out.print("<h1>This is simple servlet " + i++); out.print("</h1></BODY>"); out.close(); } } ////////////////////////////////////////////////
To deploy this code you need
- Sun Application Server or - Tomcat I advise you to download Tomcat from this site http://jakarta.apache.org/tomcat/ and read tutorial. You need serveral hours to learn about it -- eldeo start the tomcat and if you use a local tomcat installation you can now reach the webapp under http://localhost:8080/mywebapp/url-pattern (for explanation of url-pattern see my explanation of web-application. What is a web-application? Well.. basically it is a bunch of files, which can be JSP, html, java classes, jar files.. etc a webapp basically has the following directory structure: ./WEB-INF/ ./WEB-INF/classes/ (here you put the java classes of your servlet for ex. ) ./WEB-INF/lib/ (here you put any non-standard libraries you use in your java classes) ./WEB-INF/web.xml (the so called web-application deployment descriptor... see below) ./some_jsp_page.jsp (here you can put jsp pages...) ./some_web_page.html (... as well as any static content) What is contained in the web.xml? It's an xml file where you describe your web-application. It could look like this: CODE <?xml version="1.0" encoding="ISO-8859-1"?> <web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" version="2.4"> <description> An examplatory web.xml file </description> <servlet> <servlet-name>helloWorld</servlet-name> <servlet-class>my.java.package.HelloWorldServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>helloWorld</servlet-name> <url-pattern>/sayHello</url-pattern> </servlet-mapping> </web-app> If you write a servlet class my.java.package.HelloWorldServlet now, place it under WEB-INF/classes/my/java/package/HelloWorldServlet.class, make a war file called hello.war of it and then deploy it on your tomcat you have successfully written and deployed your first webapp... you can reach it under http://localhost:8080/hello/sayHello (if you have a local tomcat running) For further reading: There are many many tutorials on the web, but i would also recommend reading the servlet and the jsp specification from sun... Link to Servlet Spec Link to JSP specification have fun...
Similar Topics
Keywords : java servlets
so most of you guys know mig33. its a wap application,probaly java.most kindly to be java. does Many having tell me to make my MMORPG in java leads me to agree, But I'm not sure truly is it What is the best framework to make enterprise application using Java (J2EE)? Currently I am Hi, I want to know which is going to be a success in future. I've asked this question to CODE <HEAD> var popUpWin=0; function popUpWindow(URLStr) { I had found this site >> http://learnjava.awardspace.com This website is for people who want hi to all!!!! just like to ask if it would be possible if i could make a custom I'm trying to do a Java mobile J2me application... But I don't know what to do... Any XML and JavaGeneral XML resources * "XML, Java and the Future of the Web," Jon Bosak. The So we have a web based application running on JBoss on several clustered servers. The front end of See the next few lines containing Java Code: CODE 1. // imports 2. import How can i call pdf995 engine from Java to generate Pdf from Doc. This approach from Visual Basic Some of the installables in java comes in form of .jar file, one has to just double click or type Hi, I'm interested in game creation on websites. The game should be playable via web browser. I'm having some issues with working with objects . I get an index out of bounds ex. I thought they were completely different things. Surely javascript should be seperated from the rest... Introduction This is a code that I use to dynamically create the drop down menus. First, you have Java technology has various usage from mobile applications to Enterprise applications. I think the Hellow friends, I am creating this thread for all queries and suggestions related to java coding. If Hey there, I have made an applet that shows images moving about the screen. I have pause/resume aving some problems taking in the Java at Uni, Was wondering if anyone could shine some light on it Hey there guys! How would i go about making an object move about a fixed point for a java does anyone know of any good websites with free java tutorials for people without a lot of prior /** * Remove from the club's collection all members who * joined in the given month, This code wont work in Safari, but works fine in Firefox 2. Can someone help. The error pops up In your opinion what's the best java editor/compiler? i use jcreator le and it works nicely...... Hi, I'm doing a course called Software Design and Development for HSC in a high school in The HTML <script> tag is used to insert a JavaScript into an HTML page. Examples Write text Looking for java, servlets
|
![]() Java Servlets - Can anyone help me!? |
| ADD REPLY / Got an Opinion! | a humble request :-) | RAPID SEARCH! | Free Hosting | [X] |
|
Express your Opinions, Thoughts or Contribute your information that might help someone here. Ask your Doubts & Queries to get answers.. "Together, We enlight each other!" |
Register FREE for AD-FREE forum, Create your own topics, Ask Questions, track topics, setup subscriptions & notifications and Get a Free Website w/ Email and FTP. | 500MB Space *No Ads*, CPanel, FTP, PHP, MySQL, EMails - 100% FREE |
|