Java tutorial
package com.ns.cm; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.io.PrintWriter; import java.net.MalformedURLException; import java.net.URL; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Timestamp; 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 org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import com.ibm.json.java.JSONArray; import com.ibm.json.java.OrderedJSONObject; import com.sun.org.apache.xml.internal.security.utils.Base64; /** * Licensed Materials - Property of mail2neeraj@gmail.com Any unauthorize use without written permission * from Author (mail2neeraj@gmail.com) is strictly prohibited. */ @WebServlet(urlPatterns = { "/login", "/activate", "/myClinic", "/manage" }) public class ProvisionServlet extends HttpServlet { private static final String _COPYRIGHT = "Licensed Materials - Property of mail2neeraj@gmail.com " + "Any unauthorize use without written permission from Author (mail2neeraj@gmail.com) is strictly prohibited."; private static final long serialVersionUID = 1L; public static final Log LOG = LogFactory.getLog(ProvisionServlet.class); @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { PrintWriter out = response.getWriter(); out.println("Redirecting..."); response.setContentType("text/html"); response.setStatus(HttpServletResponse.SC_OK); response.sendRedirect(request.getContextPath()); } @Override protected void doPut(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { InputStream is = request.getInputStream(); byte[] bytes = readAll(is); String sJson = new String(bytes, "utf-8"); // json is utf-8 PrintWriter out = response.getWriter(); String a = "Reached doPut <BR> Data submitted: <BR>"; out.println(a); out.println(sJson); response.setContentType("text/html"); response.setStatus(HttpServletResponse.SC_OK); } @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // InputStream is = request.getInputStream (); // byte[] bytes = readAll (is); PrintWriter out = response.getWriter(); out.println("<HTML><BODY>"); String uid = request.getParameter("uid"); String password = request.getParameter("password"); if (uid.equalsIgnoreCase("secretUID") && password.equalsIgnoreCase("secretPW")) { out.println("<BR>Welcome back " + uid.toUpperCase()); } else { out.println("<BR> Incorrect userid/password!"); String jsForBack = "<script> function goBack() { window.history.back() } </script>"; out.println(jsForBack); out.println("<button onclick=\"goBack()\">Go Back</button>"); } out.println("</BODY></HTML>"); response.setContentType("text/html"); response.setStatus(HttpServletResponse.SC_OK); } @Override protected void doDelete(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO // PrintWriter out = response.getWriter(); out.println("Processed doDelete"); response.setContentType("text/html"); response.setStatus(HttpServletResponse.SC_OK); } public static byte[] readAll(InputStream in) throws IOException { ByteArrayOutputStream baos = new ByteArrayOutputStream(); byte[] b = null; if (baos != null) { copyStream(in, baos); b = baos.toByteArray(); } return b; } public static void copyStream(InputStream is, OutputStream os) throws IOException { byte[] buf = new byte[4096]; try { int n = 0; while (is != null && (n = is.read(buf)) > 0) os.write(buf, 0, n); } finally { try { if (is != null) is.close(); } catch (IOException e) { } try { if (os != null) os.close(); } catch (IOException e) { } } } }