Sunteți pe pagina 1din 6

Dezvoltare de proiecte WEB bazate pe serverul TOMCAT i IDE Eclipse

1. Configurare Eclipse pentru lucrul cu Tomcat 7:


a. Se deschide Eclipse in perspective Java EE
b. Se selecteaz: Window - Preferences Server Runtime Environments Add se
selecteaza Apache Tomcat v7.0 Next Tomcat installation directory Browse se
cauta directorul in care este instalat Tomcat 7 Finish
2. Creare proiect Web ce testeaza configurarea corecta a serverului Tomcat:
a. New Dynamic Web Project nume proiect Finish
b. In browserul de proiecte dublu click pe proiectul creat pentru a accesa structura acestuia
c. Pe component WebContent click dr. new JSP se introduce numele (index.jsp)
Finish

Index.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Tomcat Test</title>
</head>
<body>
<h1> Tomcat test</h1>
<p> It works!</p>
</body>
</html>

d. In browserul de proiecte, pe proiectul creat click. Dr Build path Configure build


path Libraries Add External JARs se deschide browserul local de fisiere si se
identifica API-ul javax.servlet (arhiva executabila)
e. In browserul de proiecte, pe proiectul creat click. dr. Run as Run on Server si la
prima executie a unui proiect Web in care, eventual nu s-a configurat serverul Tomcat
Manually define a new server select the server type in fereastra Apache Tomcat se
selecteaza v7.0 se bifeaz caseta cu mesajul Always use this server when running this
project Next Finish
f. in fereastra de iesire (pe optiunea selectata Servers) va aparea serverul ales
g. se deschide fereastra locala browser cu mesajul

Tomcat test

It works!
3. Lucrul cu baze de date se exemplifica printr-o aplicatie ce inregistreaza utilizatori intr-o baza de
date si permite ulterior, logarea in aplicatie pe baza datelor inregistrate:
a. Se creaza un proiect Web dynamic, ce va contine urmatoarele fisiere JSP:

index.jsp

<%@page contentType="text/html" pageEncoding="UTF-8"%>


<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Example</title>
</head>
<body>
<form method="post" action="login.jsp">
<center>
<table border="1" width="30%" cellpadding="3">
<thead>
<tr>
<th colspan="2">Login Here</th>
</tr>
</thead>
<tbody>
<tr>
<td>User Name</td>
<td><input type="text" name="uname" value="" /></td>
</tr>
<tr>
<td>Password</td>
<td><input type="password" name="pass" value="" /></td>
</tr>
<tr>
<td><input type="submit" value="Login" /></td>
<td><input type="reset" value="Reset" /></td>
</tr>
<tr>
<td colspan="2">Yet Not Registered!! <a
href="reg.jsp">Register Here</a></td>
</tr>
</tbody>
</table>
</center>
</form>
</body>
</html>

login.jsp

<%@ page import ="java.sql.*" %>


<%
String userid = request.getParameter("uname");
String pwd = "";
Class.forName("com.mysql.jdbc.Driver");
Connection con =
DriverManager.getConnection("jdbc:mysql://localhost:3306/dbname",
"root", "");
Statement st = con.createStatement();
ResultSet rs;
rs = st.executeQuery("select * from members where uname='" + userid + "'");
if (rs.next()) {
session.setAttribute("userid", userid);
//out.println("welcome " + userid);
//out.println("<a href='logout.jsp'>Log out</a>");
response.sendRedirect("success.jsp");
} else {
out.println("Invalid password <a href='index.jsp'>try again</a>");
}

%>

registration.jsp

<%@ page import ="java.sql.*" %>


<%
String user = request.getParameter("uname");
String pwd = "";
String fname = request.getParameter("fname");
String lname = request.getParameter("lname");
String email = request.getParameter("email");
Class.forName("com.mysql.jdbc.Driver");
Connection con =
DriverManager.getConnection("jdbc:mysql://localhost:3306/dbname",
"root", "");
Statement st = con.createStatement();
//ResultSet rs;
int i = st.executeUpdate("insert into members(first_name, last_name, email,
uname, pass, regdate) values ('" + fname + "','" + lname + "','" + email + "','" +
user + "','" + pwd + "', CURDATE())");
if (i > 0) {
//session.setAttribute("userid", user);
response.sendRedirect("welcome.jsp");
// out.print("Registration Successfull!"+"<a href='index.jsp'>Go to
Login</a>");
} else {
response.sendRedirect("index.jsp");
}
%>

reg.jsp

<%@page contentType="text/html" pageEncoding="UTF-8"%>


<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Registration</title>
</head>
<body>
<form method="post" action="registration.jsp">
<center>
<table border="1" width="30%" cellpadding="5">
<thead>
<tr>
<th colspan="2">Enter Information Here</th>
</tr>
</thead>
<tbody>
<tr>
<td>First Name</td>
<td><input type="text" name="fname" value="" /></td>
</tr>
<tr>
<td>Last Name</td>
<td><input type="text" name="lname" value="" /></td>
</tr>
<tr>
<td>Email</td>
<td><input type="text" name="email" value="" /></td>
</tr>
<tr>
<td>User Name</td>
<td><input type="text" name="uname" value="" /></td>
</tr>
<tr>
<td>Password</td>
<td><input type="password" name="pass" value="" /></td>
</tr>
<tr>
<td><input type="submit" value="Submit" /></td>
<td><input type="reset" value="Reset" /></td>
</tr>
<tr>
<td colspan="2">Already registered!! <a
href="index.jsp">Login Here</a></td>
</tr>
</tbody>
</table>
</center>
</form>
</body>
</html>

logout.jsp

<%
session.setAttribute("userid", null);
session.invalidate();
response.sendRedirect("index.jsp");
%>

success.jsp

<%
if ((session.getAttribute("userid") == null) || (session.getAttribute("userid")
== "")) {
%>
You are not logged in<br/>
<a href="index.jsp">Please Login</a>
<%} else {
%>
Welcome <%=session.getAttribute("userid")%>
<a href='logout.jsp'>Log out</a>
<%
}
%>

welcome.jsp

Registration is Successful.
Please Login Here <a href='index.jsp'>Go to Login</a>

Desi aplicatia presupune parole pentru utilizatorii inregistrati, eu nu am folosit decat userul root fara
parola pentru oricare utilizator inregistrat la accesarea bazei de date. Se pot crea si conturi multiple pe
DB.

Pentru ca aplicatia sa mearga trebuie intrat pe phpMyAdmin si creat baza de date dbname cu tabelul de
membri folosind urmatorul fisier SQL:

CREATE TABLE `members` (

`id` int(10) unsigned NOT NULL auto_increment,


`first_name` varchar(45) NOT NULL,

`last_name` varchar(45) NOT NULL,

`email` varchar(45) NOT NULL,

`uname` varchar(45) NOT NULL,

`pass` varchar(45) NOT NULL,

`regdate` date NOT NULL,

PRIMARY KEY (`id`)

) ENGINE=InnoDB DEFAULT CHARSET=latin1;

S-ar putea să vă placă și