Skip to content

Commit

Permalink
Db, cookie, passing hidden param
Browse files Browse the repository at this point in the history
  • Loading branch information
maheshp1987 committed Mar 11, 2017
1 parent 83fd8ce commit 8f0eb98
Show file tree
Hide file tree
Showing 8 changed files with 317 additions and 0 deletions.
70 changes: 70 additions & 0 deletions AdvanceJava/10 Database Connection using Servlets/DBServlet.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package com.db;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


public class DBServlet extends HttpServlet {
private static final long serialVersionUID = 1L;

/**
* @see HttpServlet#HttpServlet()
*/
public DBServlet() {
super();
Connection conn=null;
// TODO Auto-generated constructor stub
}

/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();

String n = request.getParameter("id");
String p = request.getParameter("name");

try {
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/demo", "root", "root");

PreparedStatement ps = con
.prepareStatement("insert into emp values(?,?)");

ps.setString(1, n);
ps.setString(2, p);

int i = ps.executeUpdate();
if (i > 0)
out.print("You are successfully registered...");

} catch (Exception e2) {
System.out.println(e2);
}

out.close();
}

/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<form action="DBServlet" method="get">

EmpID:<input type="text" name="id"/><br/>
Name:<input type="text" name="name"/><br/>
<input type="submit" value="Submit"/>
</form>
</body>
</html>
13 changes: 13 additions & 0 deletions AdvanceJava/8 Cookies Servlet/CookieDemo.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<form action="FirstCookieServlet" method="post">
Name:<input type="text" name="userName"/><br/>
<input type="submit" value="Proceed"/>
</form>
</body>
</html>
57 changes: 57 additions & 0 deletions AdvanceJava/8 Cookies Servlet/FirstCookieServlet.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package com.cookies;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


public class FirstCookieServlet extends HttpServlet {
private static final long serialVersionUID = 1L;

/**
* @see HttpServlet#HttpServlet()
*/
public FirstCookieServlet() {
super();
// TODO Auto-generated constructor stub
}

/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
response.getWriter().append("Served at: ").append(request.getContextPath());
}

/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
try{

response.setContentType("text/html");
PrintWriter out = response.getWriter();

String name=request.getParameter("userName");
out.print("Welcome "+name);

Cookie ck=new Cookie("uname",name);//creating cookie object
response.addCookie(ck);//adding cookie in the response

//creating submit button
out.print("<form action='SecondCookieServlet' method='post'>");
out.print("<input type='submit' value='Submit'>");
out.print("</form>");

out.close();

}catch(Exception e){System.out.println(e);}
}

}
48 changes: 48 additions & 0 deletions AdvanceJava/8 Cookies Servlet/SecondCookieServlet.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package com.cookies;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


public class SecondCookieServlet extends HttpServlet {
private static final long serialVersionUID = 1L;

/**
* @see HttpServlet#HttpServlet()
*/
public SecondCookieServlet() {
super();
// TODO Auto-generated constructor stub
}

/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
response.getWriter().append("Served at: ").append(request.getContextPath());
}

/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
try{

response.setContentType("text/html");
PrintWriter out = response.getWriter();

Cookie ck[]=request.getCookies();
out.print("Hello "+ck[2].getValue());

out.close();

}catch(Exception e){System.out.println(e);}
}
}
54 changes: 54 additions & 0 deletions AdvanceJava/9 Passing Hidden Variable/FirstHiddenServlet.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package com.hidden;

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 FirstHiddenServlet extends HttpServlet {
private static final long serialVersionUID = 1L;

/**
* @see HttpServlet#HttpServlet()
*/
public FirstHiddenServlet() {
super();
// TODO Auto-generated constructor stub
}

/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
try{

response.setContentType("text/html");
PrintWriter out = response.getWriter();

String name=request.getParameter("userName");
out.print("Welcome "+name);

//creating form that have invisible textfield
out.print("<form action='SecondHiddenServlet'>");
out.print("<input type='hidden' name='uname' value='"+name+"'>");
out.print("<input type='submit' value='Submit'>");
out.print("</form>");
out.close();

}catch(Exception e){System.out.println(e);}

}

/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}

}
13 changes: 13 additions & 0 deletions AdvanceJava/9 Passing Hidden Variable/HiddenParamDemo.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<form action="FirstHiddenServlet" method="post">
Name:<input type="text" name="userName"/><br/>
<input type="submit" value="Proceed"/>
</form>
</body>
</html>
47 changes: 47 additions & 0 deletions AdvanceJava/9 Passing Hidden Variable/SecondHiddenServlet.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package com.hidden;

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 SecondHiddenServlet extends HttpServlet {
private static final long serialVersionUID = 1L;

/**
* @see HttpServlet#HttpServlet()
*/
public SecondHiddenServlet() {
super();
// TODO Auto-generated constructor stub
}

/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
try{
response.setContentType("text/html");
PrintWriter out = response.getWriter();

//Getting the value from the hidden field
String name=request.getParameter("uname");
out.print("Hello "+name);

out.close();
}catch(Exception e){System.out.println(e);}
}

/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}

}

0 comments on commit 8f0eb98

Please sign in to comment.