diff --git a/login and Registration.jsp b/login and Registration.jsp new file mode 100644 index 0000000..dec3476 --- /dev/null +++ b/login and Registration.jsp @@ -0,0 +1,406 @@ +Register_1.jsp + +<%@ page language="java" contentType="text/html; charset=ISO-8859-1" + pageEncoding="ISO-8859-1"%> + + + + +Guru Registration Form + + +

Guru Register Form

+
+ + + + + + + + + + + + + + + + + + + + + + + + +
First Name
Last Name
UserName
Password
Address
Contact No
+
+ + + +Guru_register.java +package demotest; + +import java.io.IOException; + +import javax.servlet.RequestDispatcher; +import javax.servlet.ServletException; +import javax.servlet.http.HttpServlet; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +/** + * Servlet implementation class guru_register + */ +public class guru_register extends HttpServlet { + private static final long serialVersionUID = 1L; + + + protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + // TODO Auto-generated method stub + String first_name = request.getParameter("first_name"); + String last_name = request.getParameter("last_name"); + String username = request.getParameter("username"); + String password = request.getParameter("password"); + String address = request.getParameter("address"); + String contact = request.getParameter("contact"); + + if(first_name.isEmpty() || last_name.isEmpty() || username.isEmpty() || + password.isEmpty() || address.isEmpty() || contact.isEmpty()) + { + RequestDispatcher req = request.getRequestDispatcher("register_1.jsp"); + req.include(request, response); + } + else + { + RequestDispatcher req = request.getRequestDispatcher("register_2.jsp"); + req.forward(request, response); + } + } + +Register_2.jsp +<%@ page language="java" contentType="text/html; charset=ISO-8859-1" + pageEncoding="ISO-8859-1"%> + + + + +Guru Success Page + + + Welcome User!!!! + + +Register_3.jsp +<%@ page language="java" contentType="text/html; charset=ISO-8859-1" + pageEncoding="ISO-8859-1"%> + + + + +Guru Login Form + + +
+ + + + + + + + + + +
UserName
Password
+
+ + + +Guru_login.java(servlet) +package demotest; + +import java.io.IOException; + +import javax.servlet.RequestDispatcher; +import javax.servlet.ServletException; +import javax.servlet.http.HttpServlet; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +/** + * Servlet implementation class guru_login + */ +public class guru_login extends HttpServlet { + + public guru_login() { + super(); + // TODO Auto-generated constructor stub + } + + protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + // TODO Auto-generated method stub + String username = request.getParameter("username"); + String password = request.getParameter("password"); + if(username.isEmpty() || password.isEmpty() ) + { + RequestDispatcher req = request.getRequestDispatcher("register_3.jsp"); + req.include(request, response); + } + else + { + RequestDispatcher req = request.getRequestDispatcher("register_4.jsp"); + req.forward(request, response); + } + } + +} +Register_4.jsp +<%@ page language="java" contentType="text/html; charset=ISO-8859-1" + pageEncoding="ISO-8859-1"%> + + + + +Guru Logged In + + + + + +
+ <% String username = request.getParameter("username"); %> +Welcome <% out.println(username); %> User!!!! You have logged in.
Logout
+ + +Action_file.jsp + +<%@ page language="java" contentType="text/html; charset=ISO-8859-1" + pageEncoding="ISO-8859-1"%> + + + + +Guru File + + +Guru File Upload: +Select file:
+
+ +
+ +
+ + + +Action_file_upload.jsp +<%@ page language="java" contentType="text/html; charset=ISO-8859-1" + pageEncoding="ISO-8859-1"%> + <%@ page import="java.io.*,java.util.*, javax.servlet.*" %> +<%@ page import="javax.servlet.http.*" %> +<%@ page import="org.apache.commons.fileupload.*" %> +<%@ page import="org.apache.commons.fileupload.disk.*" %> +<%@ page import="org.apache.commons.fileupload.servlet.*" %> +<%@ page import="org.apache.commons.io.output.*" %> + + + + +Guru File Upload + + +<% + File file ; + int maxFileSize = 5000 * 1024; + int maxMemSize = 5000 * 1024; + String filePath = "E:/guru99/data"; + + String contentType = request.getContentType(); + if ((contentType.indexOf("multipart/form-data") >= 0)) { + + DiskFileItemFactory factory = new DiskFileItemFactory(); + factory.setSizeThreshold(maxMemSize); + factory.setRepository(new File("c:\\temp")); + ServletFileUpload upload = new ServletFileUpload(factory); + upload.setSizeMax( maxFileSize ); + try{ + List fileItems = upload.parseRequest(request); + Iterator i = fileItems.iterator(); + out.println(""); + out.println(""); + while ( i.hasNext () ) + { + FileItem fi = (FileItem)i.next(); + if ( !fi.isFormField () ) { + String fieldName = fi.getFieldName(); + String fileName = fi.getName(); + boolean isInMemory = fi.isInMemory(); + long sizeInBytes = fi.getSize(); + file = new File( filePath + "yourFileName") ; + fi.write( file ) ; + out.println("Uploaded Filename: " + filePath + fileName + "
"); + } + } + out.println(""); + out.println(""); + }catch(Exception ex) { + System.out.println(ex); + } + }else{ + out.println(""); + out.println(""); + out.println("

No file uploaded

"); + out.println(""); + out.println(""); + } +%> + + + + Uploading_1.jsp + <%@ page language="java" contentType="text/html; charset=ISO-8859-1" + pageEncoding="ISO-8859-1"%> + + + + +Guru Uploading File + + +File:
+
+ +
+ +
+ + +Guru_upload.java +package demotest; + +import java.io.File; +import java.io.IOException; +import java.util.List; + +import javax.servlet.ServletException; +import javax.servlet.http.HttpServlet; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +import org.apache.commons.fileupload.FileItem; +import org.apache.commons.fileupload.disk.DiskFileItemFactory; +import org.apache.commons.fileupload.servlet.ServletFileUpload; + +public class guru_upload extends HttpServlet { + private static final long serialVersionUID = 1L; + + public guru_upload() { + super(); + // TODO Auto-generated constructor stub + } + protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + if(ServletFileUpload.isMultipartContent(request)){ + try { + List multiparts = new ServletFileUpload(new DiskFileItemFactory()).parseRequest(request); + for(FileItem item : multiparts){ + if(!item.isFormField()){ + String name = new File(item.getName()).getName(); + item.write( new File("c:/guru/upload" + File.separator + name)); + } + } + //File uploaded successfully + request.setAttribute("gurumessage", "File Uploaded Successfully"); + } catch (Exception ex) { + request.setAttribute("gurumessage", "File Upload Failed due to " + ex); + } + }else{ + + request.setAttribute("gurumessage","No File found"); + } + request.getRequestDispatcher("/result.jsp").forward(request, response); + + } + + +} + +Result.jsp +<%@ page language="java" contentType="text/html; charset=ISO-8859-1" + pageEncoding="ISO-8859-1"%> + + + + +Guru Result + + +<% String msg = (String)request.getAttribute("message"); + out.println(msg); +%> + + +Downloading_1.jsp +<%@ page language="java" contentType="text/html; charset=ISO-8859-1" + pageEncoding="ISO-8859-1"%> + + + + +Downloading Guru Example + + +Guru Downloading FileDownload here!!! + + +Guru_download.java +package demotest; + +import java.io.FileInputStream; +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; + +/** + * Servlet implementation class guru_download + */ +public class guru_download extends HttpServlet { + private static final long serialVersionUID = 1L; + + protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + response.setContentType("text/html"); + PrintWriter out = response.getWriter(); + String gurufile = "test.txt"; + String gurupath = "c:/guru/upload/"; + response.setContentType("APPLICATION/OCTET-STREAM"); + response.setHeader("Content-Disposition", "attachment; filename=\"" + + gurufile + "\""); + + FileInputStream fileInputStream = new FileInputStream(gurupath + + gurufile); + + int i; + while ((i = fileInputStream.read()) != -1) { + out.write(i); + } + fileInputStream.close(); + out.close(); + } + + + /** + * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) + */ + protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + // TODO Auto-generated method stub + } + +}