Prac3c 4b
Prac3c 4b
CookiesHtml.html:-
<!DOCTYPE html>
<html>
<head>
<title></title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<form action="AddCookieServlet" method="post">
Name : <input type="text" name="userName"><br>
<input type="submit" value="go">
</form>
</body>
</html>
AddCookieServlet.java:-
import javax.servlet.http.Cookie;
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;
out.println("<!DOCTYPE html>");
out.println("<html>");
out.println("<head>");
out.println("<title>Servlet AddCookieServlet</title>");
TY.BSC.IT(SEM V) ENTERPRISE JAVA Roll No :46
Name: Harshad Mhatre
out.println("</head>");
out.println("<body>");
String n=request.getParameter("userName");
out.print("Welcome " + n);
Cookie ck=new Cookie("uname",n);
response.addCookie(ck);
out.print("<form action=GetCookie>");
out.print("<input type='submit' value='go'>");
out.print("</form>");
out.close();
out.println("</body>");
out.println("</html>");
}
catch(Exception e)
{
System.out.print(e);
}
}
}
GetCookie.java:-
import javax.servlet.http.Cookie;
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;
out.println("<!DOCTYPE html>");
out.println("<html>");
out.println("<head>");
out.println("<title>Servlet GetCookie</title>");
TY.BSC.IT(SEM V) ENTERPRISE JAVA Roll No :46
Name: Harshad Mhatre
out.println("</head>");
out.println("<body>");
Cookie ck[]=request.getCookies();
out.print("Hello"+ck[0].getValue());
out.close();
out.println("</body>");
out.println("</html>");
}
catch(Exception e)
{
System.out.print(e);
}
}
}
Output:-
TY.BSC.IT(SEM V) ENTERPRISE JAVA Roll No :46
Name: Harshad Mhatre
Practical 3(d):-
Create a servlet demonstrating the use of session creation and destruction. Also check whether
the user has visited this page first time or has visited earlier also using sessions.
HttpSessionServlet.java:-
import javax.servlet.http.*;
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;
out.println("<!DOCTYPE html>");
out.println("<html>");
out.println("<head>");
out.println("<title>Servlet HttpSessionServlet</title>");
out.println("</head>");
out.println("<body>");
HttpSession session =request.getSession(true);
if(session.isNew())
{
out.print("This is the first time you are visiting this page");
++counter;
}
else
{
synchronized(HttpSessionServlet.this)
{
if(counter==10)
{
TY.BSC.IT(SEM V) ENTERPRISE JAVA Roll No :46
Name: Harshad Mhatre
session.invalidate();
counter=0;
request.getSession(false);
}
else
{
out.print("You have visited this page "+(++counter)+" times");
}
}
}
out.println("</body>");
out.println("</html>");
}
}
}
Output:-
TY.BSC.IT(SEM V) ENTERPRISE JAVA Roll No :46
Name: Harshad Mhatre
Practical 4 (a):-
Create a Servlet application to upload a file.
Index.html:-
<html>
<head>
<title>TODO supply a title</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<form action="FileUploadServlet" method="post" enctype="multipart/form-data">
Select File to Upload:<input type="file" name="file" id="file">
Destination <input type="text" value="/" name="destination">
<br>
<input type="submit" value="Upload file" name="upload" id="upload">
</form>
</body>
</html>
FileUploadServlet.java:-
package fileservletapp;
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;
import java.io.*;
import javax.servlet.*;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.http.*;
@MultipartConfig
public class FileUploadServlet extends HttpServlet {
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
TY.BSC.IT(SEM V) ENTERPRISE JAVA Roll No :46
Name: Harshad Mhatre
response.setContentType("text/html");
PrintWriter out = response.getWriter();
String path=request.getParameter("destination");
Part filePart=request.getPart("file");
String filename=filePart.getSubmittedFileName().toString();
out.print("<br><br><hr> file name: "+filename);
OutputStream os=null;
InputStream is=null;
try
{
os=new FileOutputStream(new File(path+File.separator+filename));
is=filePart.getInputStream();
int read=0;
byte[] b=new byte[1024];
while ((read = is.read(b)) != -1)
{
os.write(b, 0, read);
}
out.println("<br>file uploaded successfully...!!!");
}
catch(FileNotFoundException e)
{
out.print(e);
}
}
}
Output:-
TY.BSC.IT(SEM V) ENTERPRISE JAVA Roll No :46
Name: Harshad Mhatre
TY.BSC.IT(SEM V) ENTERPRISE JAVA Roll No :46
Name: Harshad Mhatre
Practical 4 (b):-
Create a Servlet application to download a file.
Index.html
<html>
<head>
<title>TODO supply a title</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<h1> File Download Application </h1>
Click <a href="FileDownloadServlet?filename=SampleChapter.pdf">Sample Chapter</a>
<br/><br/>
Click <a href="FileDownloadServlet?filename=TOC.pdf">Table Of Contents</a>
</body>
</html>
FileDownloadServlet.java:-
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;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("APPLICATION/OCTET-STREAM");
String filename = request.getParameter("filename");
ServletContext context = getServletContext();
InputStream is = context.getResourceAsStream("/" + filename);
ServletOutputStream os = response.getOutputStream();
TY.BSC.IT(SEM V) ENTERPRISE JAVA Roll No :46
Name: Harshad Mhatre
response.setHeader("Content-Disposition","attachment; filename=\"" + filename + "\"");
int i;
byte b[]=new byte[1024];
while ((i=is.read(b)) != -1)
{
os.write(b);
}
is.close();
os.close();
}
}
Output:-
TY.BSC.IT(SEM V) ENTERPRISE JAVA Roll No :46
Name: Harshad Mhatre