JSP Devlopment

JSP Devlopment:

Registration Form in JSP

For making enlistment frame, you should have a table in the database. You can compose the database rationale in JSP record, yet isolating it from the JSP page is better approach. Here, we will utilize DAO, Factory Method, DTO and Singletion configuration designs.

  • index.jsp
  • User.java
  • process.jsp
  • Provider.java
  • ConnectionProvider.java
  • RegisterDao.java
Example of Registration Form:
  • First create the table in the Oracle database:

CREATE TABLE  “USER432”

(    “NAME” VARCHAR2(4000),

“EMAIL” VARCHAR2(4000),

“PASS” VARCHAR2(4000)

)

/

index.jsp

<form action=”process.jsp”>

<input type=”text” name=”uname” value=”Name…” onclick=”this.value=””/><br/>

<input type=”text” name=”uemail”  value=”Email ID…” onclick=”this.value=””/><br/>

<input type=”password” name=”upass”  value=”Password…” onclick=”this.value=””/><br/>

<input type=”submit” value=”register”/>

</form>

process.jsp

<%@page import=”bean.RegisterDao”%>

<jsp:useBean id=”obj” class=”bean.User”/>

<jsp:setProperty property=”*” name=”obj”/>

<%

int status=RegisterDao.register(obj);

if(status>0)

out.print(“You are successfully registered”);

%>

User.java

public class User {  package bean;

private String uname,upass,uemail;

public String getUname() {

return uname;

}

public void setUname(String uname) {

this.uname = uname;

}  public String getUpass() {

return upass;

}  public void setUpass(String upass) {

this.upass = upass;

}

public String getUemail() {

return uemail;

}

public void setUemail(String uemail) {

this.uemail = uemail;  }

}

Provider.java

package bean;

public interface Provider {

String DRIVER=”oracle.jdbc.driver.OracleDriver”;

String CONNECTION_URL=”jdbc:oracle:thin:@localhost:1521:xe”;

String USERNAME=”system”;

String PASSWORD=”oracle”;

}

ConnectionProvider.java

package bean;

import java.sql.*;

import static bean.Provider.*;

public class ConnectionProvider {

private static Connection con=null;

static{

try{

Class.forName(DRIVER);

con=DriverManager.getConnection(CONNECTION_URL,USERNAME,PASSWORD);

}catch(Exception e){}

}

public static Connection getCon(){

return con;

}

}

RegisterDao.java:

package bean;

import java.sql.*;

public class RegisterDao {

public static int register(User u){

int status=0;

try{

Connection con=ConnectionProvider.getCon();

PreparedStatement ps=con.prepareStatement(“insert into user432 values(?,?,?)”);

ps.setString(1,u.getUname());

ps.setString(2,u.getUemail());

ps.setString(3,u.getUpass());

status=ps.executeUpdate();

}catch(Exception e){}

return status;

}

}

Login and Logout:

The DAO (Data Access Object), Factory method and DTO (Data Transfer Object) design patterns.

  • index.jsp
  •  login.jsp
  • loginprocess.jsp
  • LoginBean.java
  • Provider.java
  • ConnectionProvider.java
  • LoginDao.java

 Example:

logout.jsp  profile.jsp CREATE TABLE  “USER432”

(    “NAME” VARCHAR2(4000),

.          “EMAIL” VARCHAR2(4000),

“PASS” VARCHAR2(4000)

)

/

index.jsp:

<a href=”login.jsp”>login</a>|

<a href=”logout.jsp”>logout</a>|

<a href=”profile.jsp”>profile</a>

login.jsp:

<%@ include file=”index.jsp” %>

<hr/>

<h3>Login Form</h3>

<%

String profile_msg=(String)request.getAttribute(“profile_msg”);

if(profile_msg!=null){

out.print(profile_msg);

}

String login_msg=(String)request.getAttribute(“login_msg”);

if(login_msg!=null){

out.print(login_msg);

}

%>

<br/>

<form action=”loginprocess.jsp” method=”post”>

Email:<input type=”text” name=”email”/><br/><br/>

Password:<input type=”password” name=”password”/><br/><br/>

<input type=”submit” value=”login”/>”

</form>

loginprocess.jsp:

<%@page import=”bean.LoginDao”%>

<jsp:useBean id=”obj” class=”bean.LoginBean”/>

<jsp:setProperty property=”*” name=”obj”/>

<%

boolean status=LoginDao.validate(obj);

if(status){

out.println(“You r successfully logged in”);

session.setAttribute(“session”,”TRUE”);

}

else

{

out.print(“Sorry, email or password error”);

%>

<jsp:include page=”index.jsp”></jsp:include>

<%

}

%>

LoginBean.java:

package bean;

public class LoginBean {

private String email,pass;

public String getEmail() {

return email;

}       public void setEmail(String email) {

this.email = email;

}

public String getPass() {

return pass;

}

public void setPass(String pass) {

this.pass = pass;

}

}

Provider.java:

package bean;

public interface Provider {

String DRIVER=”oracle.jdbc.driver.OracleDriver”;

String CONNECTION_URL=”jdbc:oracle:thin:@localhost:1521:xe”;

String USERNAME=”system”;

String PASSWORD=”oracle”;

}

ConnectionProvider.java

package bean;

import java.sql.*;

import static bean.Provider.*;

public class ConnectionProvider {

private static Connection con=null;

static{

try{

Class.forName(DRIVER);

con=DriverManager.getConnection(CONNECTION_URL,USERNAME,PASSWORD);

}catch(Exception e){}

}  public static Connection getCon(){

return con;  }

}

LoginDao.java

package bean;

import java.sql.*;

public class LoginDao {

public static boolean validate(LoginBean bean){

boolean status=false;

try{

Connection con=ConnectionProvider.getCon();

PreparedStatement ps=con.prepareStatement(

“select * from user432 where email=? and pass=?”);

ps.setString(1,bean.getEmail());

ps.setString(2, bean.getPass());

ResultSet rs=ps.executeQuery();

status=rs.next();

}catch(Exception e){}

return status;

}

}
Uploading file to the server using JSP:

There are numerous approaches to transfer the document to the server. One of the path is by the MultipartRequest class. For utilizing this class you need the cos.jar document. In this illustration, we are giving the cos.jar document alongwith the code.

MultipartRequest class : It is an utility class to deal with the multipart/shape information ask. There are numerous constructors characterized in the MultipartRequest class.

Commonly used Constructors of MultipartRequest class:
  • MultipartRequest(HttpServletRequest request, String saveDirectory)
  • MultipartRequest(HttpServletRequest request, String saveDirectory, int maxPostSize)
  • MultipartRequest(HttpServletRequest request, String saveDirectory, int maxPostSize, String encoding)
index.jsp

<form action=”upload.jsp” method=”post” enctype=”multipart/form-data”>

Select File:<input type=”file” name=”fname”/><br/>

<input type=”image” src=”MainUpload.png”/>

</form>

 upload.jsp:

<%@ page import=”com.oreilly.servlet.MultipartRequest” %>

<%

MultipartRequest m = new MultipartRequest(request, “d:/new”);

out.print(“successfully uploaded”);

%>

Downloading file from the server using JSP :

In this illustration, we will download the jsp record. Be that as it may, you may download any document. For downloading the record from the server, you ought to determine the substance sort named APPLICATION/OCTET-STREAM.

index.jsp

<a href=”download.jsp”>download the jsp file</a>

download.jsp

<%

String filename = “home.jsp”;

String filepath = “e:\\”;

response.setContentType(“APPLICATION/OCTET-STREAM”);

response.setHeader(“Content-Disposition”,”attachment; filename=\”” + filename + “\””);

java.io.FileInputStream fileInputStream=new java.io.FileInputStream(filepath + filename);

int i;

while ((i=fileInputStream.read()) != -1) {

out.write(i);

}

fileInputStream.close();

%>

 
Drop Us A Query

Trending Courses
  • Python and Django Online Training
  • Python is a dynamic interrupted language which is used in wide varieties of applications. It is very interactive object oriented and high-level programming language.

  • Tableau Online Training
  • Tableau is a Software company that caters interactive data visualization products that provide Business Intelligence services. The company’s Head Quarters is in Seattle, USA.

  • MicroStrategy Online Training
  • Micro Strategy is one of the few independent and publicly trading Business Intelligence software provider in the market. The firm is operational in 27 Countries around the globe.

  • PEGA (PRPC) 7.2 Certification Online Training
  • Pega Systems Inc. is a Cambridge, Massachusetts based Software Company. It is known for developing software for Customer Relationship Management (CRM) and Business process Management (BPM).

  • Workday Online Training
  • Workday specialises in providing Human Capital Management, Financial Management and payroll in online domain.It is a major web based ERP software vendor.

  • Power BI Online Training
  • Power BI is business analytics service by Microsoft. With Power BI, end users can develop reports and dashboards without depending on IT staff or Database Administrator.


100% Secure Payments. All major credit & debit cards accepted.