-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
maheshp1987
committed
Mar 11, 2017
1 parent
83fd8ce
commit 8f0eb98
Showing
8 changed files
with
317 additions
and
0 deletions.
There are no files selected for viewing
70 changes: 70 additions & 0 deletions
70
AdvanceJava/10 Database Connection using Servlets/DBServlet.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
} |
15 changes: 15 additions & 0 deletions
15
AdvanceJava/10 Database Connection using Servlets/DemoDBConnect.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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);} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
54
AdvanceJava/9 Passing Hidden Variable/FirstHiddenServlet.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
13
AdvanceJava/9 Passing Hidden Variable/HiddenParamDemo.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
47
AdvanceJava/9 Passing Hidden Variable/SecondHiddenServlet.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
} |