Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Jsp Implementing CSS and JavaScript in JSP File

Related Tutorials


 
 

Share on Google+Share on Google+

Implementing CSS and JavaScript in JSP File

Advertisement
This example shows how to implement css and JavaScript in jsp. Here we have created three main files that is cssScript.jsp, validate.js and style.css.

Implementing CSS and JavaScript in JSP File

     

This example shows how to implement css and JavaScript in jsp. Here we have created three main files that is cssScript.jsp, validate.js and style.css. 

In "cssScript.jsp" file, the following code imports style.css file from css folder

<link rel="stylesheet" href="../css/style.css" type="text/css"></link>

and the code below imports validate.js file from script folder.

<script language="JavaScript" type="text/JavaScript" src="../script/validate.js"></script>

The style.css file is used to implement the style for the jsp page i.e. look and feel for the page and the validate.js file is used for field validation.

Directory Structure of css and script file

cssScript.jsp

<%@page language="java" session="true" 
contentType="text/html;charset=ISO-8859-1" %> 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 
ransitional//EN" "http://www.w3.org/
  TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<font color="blue">Please Enter User Name and Password </font><br><br>

<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Implementing css and javascript</title>

<link rel="stylesheet" href="../css/style.css" type="text/css"></link>
<script language="JavaScript" type="text/JavaScript" 
src="../script/validate.js"></script>

</head>

<form enctype="multipart/form-data" onSubmit="return validate(this)" method="post" action="Welcome.html">
  <table border = "0">
  <tr align="left" valign="top">
  <td>User Name:</td>
  <td><input type="text" name ="user" class="inputbox"/></td>
  </tr>
  <tr align="left" valign="top">
  <td>Password:</td>
  <td><input type="password" name ="pass" class="inputbox"/></td>
  </tr>
  <tr align="left" valign="top">
  <td></td>
  <td><input type="submit" name="submit" 
  value="submit" class="submitButton"/></td>
  </tr>
  </table>
</form>

style.css

body{
  background-color: #CCCCCC;
  border: 2px solid #009900;
  height: 130px;
  width: 300px;
  margin-left: 15px;
  padding-left:5px;
  padding-right:5px;
}

.inputbox {
  background-color:#FFFFFF; 
  color: #333333;
  width:150px;
  border-width:1px;
  border-style:solid;
  border-color:#808080;
}

.inputbox:hover {
  border-width:1px;
  border-style:solid;
  border-color:#11A3EA;
}

.inputbox:focus {
  border-width:1px;
  border-style:solid;
  border-color:#11A3EA;
  color: #0F4987;
}

.submitButton {
  background-color: #ffffff;
  color: #000000;
  background: #ffffff;
  border: 1px solid #cccccc; 
  text-align: center;
  width: auto;
  padding: 2px 3px 2px 3px;
}

validate.js

function validate(theForm){
  if(theForm.user.value.length==0){
  alert("UserId can't be blank");
  theForm.user.focus();
   return false;
  }else if(theForm.pass.value.length==0){
  alert("Password can't be blank");
  theForm.pass.focus();
  return false;
  }else if(theForm.pass.value.length<6){
  alert("Password length can't be less than 6 char");
  theForm.pass.focus();
  return false;
  }
}

Welcome.html

<html>

  <head>
  <title>Welcome Page</title>
  </head>

  <body>
  <P><h1><font color='green'> Welome User </font></h1></P>
  </body>

</html>

Running program through this url- http://localhost:8080/ExampleServlet/jsp/cssScript.jsp  the page is displayed as below:

If user successfully enters the valid input value then the following Welcome.html page appears.

Download Source Code

Advertisement

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: August 1, 2008

Related Tutorials

Discuss: Implementing CSS and JavaScript in JSP File   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:5
MAITREYEE DHAR
February 26, 2013
java

how to connect .jsp page and .css page? how to design jsp page using css?
Tejas N. Mahajan
March 22, 2012
Implementing JavaScript in JSP File

Hi, I am doing the project in JSP in which I am going to use JS file which validates the JSP page compnont for devnagari input, But I have probleam regarding to implementing the JS file in JSP Please help me.
Szymon
May 30, 2012
Thx

Man, big thanks. At last someone who knows how to do the thing, not how to deliberate with a very little knowledge.
srinivasa.v
October 21, 2012
about program

please tell me how to give valid input values for username ans passsword
rose
April 28, 2013
jsp with css

how to create a form using css in jsp?
DMCA.com