1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
package controller; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class CountDown extends HttpServlet { private static final long serialVersionUID = 1L; public CountDown() { super(); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); PrintWriter out = response.getWriter(); out.println("<html><head><title>Count Down</title></head>"); out.println("<center><h1>Count Down Example</h1></center>"); if(request.getParameter("count") != null) { int strCount = Integer.parseInt(request.getParameter("count")) - 1; if(strCount == 0) { out.println("<a href=\"" + getURLWithContextPath(request) + "\">Start Again...!!!</a>"); } else { response.setHeader("REFRESH", "1; " + getURLWithContextPath(request) + "?count=" + strCount); out.println("<h1>" + strCount + "</h1>"); } } else { out.println("<form method=\"GET\" action=\"" + getURLWithContextPath(request) + "\">" + "<span style=\"margin-right:5px;\">Set Count</span>" + "<input type=\"text\" name=\"count\" /> <input type=\"submit\" value=\"Start Counter\" />" + "</form>"); } out.println("</html>"); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } public static String getURLWithContextPath(HttpServletRequest request) { return request.getScheme() + "://" + request.getServerName() + ":" +request.getServerPort() + request.getRequestURI(); } } |
Category Archives: Java
Download Controller – JavaEE Servlet
Here is small piece of java code to generate binary stream which tells browser to save file as attachment. I believe that this code will help java beginners to understand HttpServletResponse of the Servlets.
Read More: http://commons.apache.org/fileupload/
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
package com.gaurangjadia.code.controllers; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.OutputStream; import javax.servlet.ServletConfig; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class Download extends HttpServlet { private static final long serialVersionUID = 1L; public Download() { super(); } public void init(ServletConfig config) throws ServletException { } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { try{ String fileName = request.getParameter("name"); String path = request.getSession().getServletContext().getRealPath("/WEB-INF/upload/"); //strFileName: Name of the file which is being downloaded and which exist under /WEB-INF/upload/ path if(fileName.equals("strFileName")){ path += "/" + fileName; File file = new File(path); Integer intFileLength = (int)(file.length() / 1024) + 1; response.setContentType(new MimetypesFileTypeMap().getContentType(file)); response.setHeader("Content-Disposition", "attachment; filename=" + fileName); FileInputStream in = new FileInputStream(path); OutputStream out = response.getOutputStream(); byte buf[] = new byte[intFileLength]; int bytesRead = 0; while( (bytesRead = in.read(buf)) > 0 ){ out.write(buf, 0, bytesRead); } out.close(); in.close(); } else{ request.getRequestDispatcher("/WEB-INF/download.jsp").forward(request, response); } } catch(Exception ex){ request.getRequestDispatcher("/WEB-INF/download.jsp").forward(request, response); } } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } } |
Connect JavaEE Application to MySQL Database on GoDaddy Shared Linux Hosting Server
I was trying to find some example to connect MySQL database with my Java Shared Hosting Plan on GoDaddy.com but I could not. Well, I don’t want to say that GoDaddy has the best plan for hosting Java application. I still can not figure out how to use JSTL, custom url-pattens in the web.xml and forward requests from a servlet to JSP page. Also, we have to put http://<domain_name>/servlet/<servlet_name> to access servlet. But, here is simple working example to show you database connection from hosting to MySQL server on GoDaddy Shared Hosting.
Take a look at the example at link : GoDaddyJavaMySQL
DBConnector.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
package com.gaurangjadia.code.base; import java.sql.Connection; import java.sql.DriverManager; public class DBConnector { private String host = ""; private int port = 3306; private String databasename = ""; private String username = ""; private String password = ""; private String connectionstring = ""; public DBConnector(String hostname, int port, String db, String username, String password){ this.setHost(hostname); this.setPort(port); this.setDatabasename(db); this.setUsername(username); this.setPassword(password); this.setConnectionstring("jdbc:mysql://" + getHost() + ":" + getPort() + "/" + getDatabasename()); } public Connection connect() throws Exception{ Class.forName("com.mysql.jdbc.Driver"); Connection objConnection = DriverManager.getConnection(this.getConnectionstring(), this.getUsername(), this.getPassword()); return objConnection; } public String getHost() { return host; } public void setHost(String host) { this.host = host; } public int getPort() { return port; } public void setPort(int port) { this.port = port; } public String getDatabasename() { return databasename; } public void setDatabasename(String databasename) { this.databasename = databasename; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getConnectionstring() { return connectionstring; } public void setConnectionstring(String connectionstring) { this.connectionstring = connectionstring; } } |
GoDaddyJavaMySQL.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
package com.gaurangjadia.code.controllers; import java.io.IOException; import java.io.PrintWriter; import java.sql.Connection; import java.sql.ResultSet; import java.sql.Statement; import javax.servlet.ServletConfig; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import com.gaurangjadia.code.base.DBConnector; @WebServlet(description = "Main Controller to handle all requests", urlPatterns = { "/index.html" }) public class GoDaddyJavaMySQL extends HttpServlet { private static final long serialVersionUID = 1L; private DBConnector objDBConnector; public GoDaddyJavaMySQL() { super(); } public void init(ServletConfig config) throws ServletException { try { objDBConnector = new DBConnector("server_ip", 3306, "database_name", "user_name", "password"); } catch (Exception ex) { ex.printStackTrace(); } } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { PrintWriter out = response.getWriter(); try { response.setContentType("text/html; charset=ISO-8859-1"); Connection conn = objDBConnector.connect(); Statement strQuery = conn.createStatement(); ResultSet rs = strQuery.executeQuery("SELECT * FROM table_name"); out.write("<html><head><title>Connect JavaEE Application to MySQL Database on GoDaddy Shared Linux Hosting Server </title></head><body>"); out.write("<h2>Connect JavaEE Application to MySQL Database on GoDaddy Shared Linux Hosting Server.</h2>"); out.write("<table border=\"1\">"); while(rs.next()){ out.write("<tr><td>" + rs.getString("Column_1") + "</td><td>" + rs.getString("Column_2") + "</td><td>" + rs.getString("Column_3") + "</td></tr>"); } out.write("</table>"); out.write("</body></html>"); } catch (Exception ex) { ex.printStackTrace(out); } } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } } |
web.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
< ?xml version="1.0" encoding="UTF-8"?> <web -app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> <display -name>GoDaddyJavaMySQL</display> <welcome -file-list> </welcome><welcome -file>index.html</welcome> <welcome -file>index.htm</welcome> <welcome -file>index.jsp</welcome> <welcome -file>default.html</welcome> <welcome -file>default.htm</welcome> <welcome -file>default.jsp</welcome> <servlet> <description></description> <display -name>GoDaddyJavaMySQL</display> </servlet><servlet -name>GoDaddyJavaMySQL</servlet> <servlet -class>com.gaurangjadia.code.controllers.GoDaddyJavaMySQL</servlet> <servlet -mapping> </servlet><servlet -name>GoDaddyJavaMySQL</servlet> <url -pattern>/GoDaddyJavaMySQL</url> </web> |