Jsp ve Servlet hakkında ayrıntılı bilgiye

linklerinden erişebilirsiniz 😉

Anlatılacak olan web uygulaması session scope’da çalışan sisteme film ekleyen, silen, listeleyen ve güncelleyen bir uygulamadır. MVC yapısını sağlamaktadır. Uygulamanın anasayfası:

Aşağıda yer alan kod start.jsp adlı dosyanın içeriğidir. Uygulamanın başlayacağı nokta tam olarak burasıdır. Projeye sağ tıklayarak New->Jsp File seçeneğiyle bir jsp dosyası açılabilir.  Kod içerisinde jsp’nin table bileşeni kullanılarak sayfa bölümlere ayrılmaktadır. Sayfa yerleşimin şekilde görüldüğü gibi olmasını sağlayan budur. Table yerine div kulanılarak da sayfa bölümlenebilirdi. Jsp’nin include page bileşeni kullanılarak başlığın altında yer alan 3 kısım eklenmektedir. Sağ ve sol kısımlarda yer alan kısımlar açılan diğer sayfalarda da yer alacaktır.

<!DOCTYPE HTML PUBLIC “-//W3C//DTD HTML 4.0 Transitional//EN”>
<html>
<head>
<meta http-equiv=”Content-Type” content=”text/html; charset=ISO-8859-1″>
<title>Menu</title>
<link rel=STYLESHEET
href=”./extra/jsp.css”
type=”text/css”>
</head>
<body>
<table  border=”3px” cellpadding=”0″ cellspacing=”0″ width=”100%”>
<tbody>
<tr>
<td colspan=”3″ width=”100%”>
<p> Welcome to Movie Archive</p>
</td>
</tr>
<tr>
<td width=”20%”>
<jsp:include page =”sinemalar.html” flush =”true”/>
</td>
<td width=”60%” >
<jsp:include page =”menu.jsp” flush =”true”/>
</td>
<td width=”20%”>
<jsp:include page =”Links.html” flush =”true”/>
</td>
</tr>
</tbody>
</table>
</body>
</html>

Sayfaların font ve renklerini belirlememizi sağlayan css dosyası ise aşağıda yer almaktadır. Tablonun font ve renkleri de burada belirlenmiştir.

body { background-color: #33ccff;
font-family:Arial;}
UL { margin-top: 0;
border-top-width: 0;
padding-top: 0
}
.TOC { font-size: 20%;
font-weight: bold;
font-family: Arial, Helvetica, sans-serif;
text-align:center;
}
.pageHeader{
font-size : 200%;
font-family: Broadway;
text-align:center;
color:#330099;
}
.header {
font-weight: bold;
text-align : center;
color:#330099;
}
.cntr{
text-align: center;
}
.link{
font-size:80%;
text-align: center;
font-family:Calibri;
}
.link2{
font-size:100%;
text-align: center;
font-family:Calibri;
color:#000099;
}
.big{
font-family: BroadWay;
font-size: 2em;
text-align: center;
color:#330099;
}
.message{
font-size : 125%;
font-family: Arial;
text-align:center;
}
TH.COLORED { background-color: #FFAD00
}
TR.COLORED { background-color: #FFAD00
}
TH.TITLE   { background-color: #EF8429;
font-size: 28px;
font-family: Arial, Helvetica, sans-serif;
}
TD.TEXT { font-family:Arial;
font-size: .9em;
padding: 5px;
background-color: #dddddd;
}
TH.TEXT{ font-family:Arial;
font-size: .9em;
padding: 5px;
background-color: #dddddd;
}

Start.jsp’ye dahil edilen sayfalardan işlevsel olarak daha az içeriğe sahip olması bakımından öncelikle sinemalar.html ve Links.html ‘in içeriğini vermenin daha uygun olduğunu düşünüyorum.

sinemalar.html

<!DOCTYPE html PUBLIC “-//W3C//DTD HTML 4.01 Transitional//EN” “http://www.w3.org/TR/html4/loose.dtd”&gt;
<html>
<head>
<meta http-equiv=”Content-Type” content=”text/html; charset=UTF-8″>
<title>Sinemalar</title>
<link rel=STYLESHEET
href=”./extra/jsp.css”
type=”text/css”>
</head>
<body>
<script type=”text/javascript” src=”http://widget.sinemalar.com/scriptcomp/widget.js”></script&gt;
<script type=”text/javascript”>writeMoviesOfCurrentWeek(‘0c2456’,”);</script>
<div style=”width: 225px; margin-top: -5px; text-align: center; font-family: verdana;”>
<a title=”sinema” href=”http://www.sinemalar.com&#8221; style=”color:black;font-size: 9px;” target=”_blank” id=”anchor”></a></div>
</body>
</html>

Kod içerisinde yer alan javascript kodu sinemalar.com’dan da elde edilebilir. Gündemde olan filmlerin geçişini sağlayan bir kod parçasıdır.
Sağ kısımda yer alan Links.html ise filmlerle ilgili çeşitli sitelere link vermektedir. <a href=”http://www.imdb.com/”&gt;  href  tırnak içerisinde yer alan dizgenin link olarak verilmesini sağlamaktadır.
<!DOCTYPE html PUBLIC “-//W3C//DTD HTML 4.01 Transitional//EN” “http://www.w3.org/TR/html4/loose.dtd”&gt;
<html>
<head>
<meta http-equiv=”Content-Type” content=”text/html; charset=UTF-8″>
<title>Links</title>
<link rel=STYLESHEET
href=”./extra/jsp.css”
type=”text/css”>
</head>
<body>
IMDb</a></p>
IMDb’s Top 250</a></p>
IMDb’s Bottom 100</a></p>
Sinamalar.com</a></p>
BeyazPerde.com</a></p>
MyBilet.com</a></p>
fragmans.com</a></p>
</body>
</html>
Asıl önemli nokta bundan sonra başlamaktadır Jsp’den ilgili servlete yönlendirme ve servlette de jsp’den gelen içeriğe göre ilgili jsp sayfasına yönlendirme işlemi yapılmaktır. Jsp’den servlet’e yönlendirme işlemi jsp’de form’un action’ını yönlendirilmek istenen servlet’in ismini vermemiz gerekmektedir. Ayrıca form’da method kısmına da servlette alınacak olan method ismi verilmelidir.  Aşağıda yer alan kod parçası menu seçeneğini radio button olarak almakta ve MenuSelector.java’ya yollamaktadır. Servlet, projeye sağ tıklayarak New->Servlet şeklinde ekleyebiliriz.
<!DOCTYPE HTML PUBLIC “-//W3C//DTD HTML 4.0 Transitional//EN”>
<html>
<head>
<title>Movie Archive Site</title>
<link rel=STYLESHEET
href=”./extra/jsp.css”
type=”text/css”>
</head>
<body>
<form action =”MenuSelector” method =”get”>
<table border= “3px groove” align=”center” >
<tr>
<td><input type = “radio” name =menuItem value =”Add”/></td>
<td>Add</td>
</tr>
<tr>
<td><input type = “radio” name =menuItem value =”Delete”/></td>
<td>Delete</td>
</tr>
<tr>
<td><input type = “radio” name =menuItem value =”List”/></td>
<td>List</td>
</tr>
<tr>
<td><input type = “radio” name =menuItem value =”Update”/></td>
<td>Update</td>
</tr>
<tr>
<td colspan =”2″>
<input type =”submit” value =”Choose”/> </td>
</tr>
</table>
</form>
</body>
</html>
MenuSelector.java
package ymlab.proje1.servlets;
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;
import javax.servlet.http.HttpSession;
import javax.servlet.*;
/**
* Servlet implementation class MenuSelector
*/
public class MenuSelector extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* Default constructor.
*/
public MenuSelector() {
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
String menuItem = request.getParameter( “menuItem” );
String address = menuItem+”.jsp”;
RequestDispatcher dispatcher =
request.getRequestDispatcher(address);
dispatcher.forward(request, response);
}
}
Burda ilgili jsp’ye yönlendirmeyi sağlayan kod bloğu aşağıda yer almaktadır. Yönlendirmeyi RequestDispatcher yapmaktadır.
String menuItem = request.getParameter( “menuItem” );
String address = menuItem+”.jsp”;
RequestDispatcher dispatcher =
request.getRequestDispatcher(address);
dispatcher.forward(request, response);
Diğer jsp ve servletler de aynı şekilde birbirlerini çağırmaktadır.
Add.jsp
<!DOCTYPE HTML PUBLIC “-//W3C//DTD HTML 4.0 Transitional//EN”>
<html>
<head>
<title>Enter Movie Features JSP</title>
<link rel=STYLESHEET
href=”jsp.css”
type=”text/css”>
</head>
<body>
<table  border=”3px” cellpadding=”0″ cellspacing=”0″ width=”100%”>
<tbody>
<tr>
<td colspan=”3″ width=”100%”>
<p>Movie Addition</p>
</td>
</tr>
<tr>
<td width=”20%”>
<jsp:include page =”sinemalar.html” flush =”true”/>
</td>
<td width=”60%” >
<form action =”Add” method =”get”>
<p>Please type the movie features:</p>
<table border= “3px groove” align=”center” >
<tr>
<td> Movie Id</td>
<td>
<input type =”text” name =”MovieID” />
</td>
</tr>
<tr>
<td> Movie Name</td>
<td>
<input type =”text” name =”MovieName”/>
</td>
</tr>
<tr>
<td> Movie Category </td>
<td>
<input type =”text” name =”MovieCategory”/>
</td>
</tr>
<tr>
<td colspan =”2″>
<input type =”submit” value =”Submit”/>
</td>
</tr>
</table>
</form>
</td>
<td width=”20%”>
<jsp:include page =”Links.html” flush =”true”/>
</td>
</tr>
</tbody>
</table>
</body>
</html>

Delete.jsp
<!DOCTYPE HTML PUBLIC “-//W3C//DTD HTML 4.0 Transitional//EN”>
<html>
<head>
<title>Enter Movie Features JSP</title>
<link rel=STYLESHEET
href=”./extra/jsp.css”
type=”text/css”>
</head>
<body>
<table  border=”3px” cellpadding=”0″ cellspacing=”0″ width=”100%”>
<tbody>
<tr>
<td colspan=”3″ width=”100%”>
<p>Movie Deletion</p>
</td>
</tr>
<tr>
<td width=”20%”>
<jsp:include page =”sinemalar.html” flush =”true”/>
</td>
<td width=”60%” >
<form action =”Delete” method =”get”>
<p class =”message”>Please enter the movie id:</p>
<table border=”3px groove” align =”center”>
<tr>
<td> Movie Id </td>
<td>
<input type =”text” name =”MovieID” />
</td>
<tr>
<td colspan=”2″>
<input type =”submit” value =”Submit”/>
</td>
</tr>
</table>
</form>
</td>
<td width=”20%”>
<jsp:include page =”Links.html” flush =”true”/>
</td>
</tr>
</tbody>
</table>
</body>
</html><!–end XHTML document –>
List.jsp
<!DOCTYPE HTML PUBLIC “-//W3C//DTD HTML 4.0 Transitional//EN”>
<%@page import =”java.util.*”%>
<%@page import =”ymlab.proje1.servlets.*”%>
<html>
<head>
<title>Movie List</title>
<link rel=STYLESHEET
href=”./extra/jsp.css”
type=”text/css”>
</head>
<body>
<table  border=”3px” cellpadding=”0″ cellspacing=”0″ width=”100%”>
<tbody>
<tr>
<td colspan=”3″ width=”100%”>
<p class = “pageHeader”>Movie List</p>
</td>
</tr>
<tr>
<td width=”20%”>
<jsp:include page =”sinemalar.html” flush =”true”/>
</td>
<td width=”60%” style =”vertical-align:top” >
<table border= “3px groove” align=”center” >
<thead>
<tr>
<th style =”width: 200px;”  class=”TEXT”>Movie ID</th>
<th style =”width: 200px;”  class=”TEXT”>Movie Name</th>
<th style =”width: 200px;”  class=”TEXT”>Movie Category</th>
<th style =”width: 50px;”  class=”TEXT”>Delete</th>
<th style =”width: 50px;”  class=”TEXT”>Update</th>
</tr>
</thead>
<tbody>
<%// start scriptlet
Enumeration valueNames;
if( session != null)
valueNames = session.getAttributeNames();
else
valueNames = null;
if( valueNames != null&&
valueNames.hasMoreElements() ) {
String ID;
MovieBean mb = new MovieBean(“”,””,””);
while( valueNames.hasMoreElements() ) {
ID = valueNames.nextElement().toString();
mb = (MovieBean)session.getAttribute( ID );
%>
<tr>
<td><%=mb.getId()%></td>
<td><%=mb.getName() %></td>
<td><%=mb.getCategory() %></td>
<form action =”Delete” method =”get”>
<td>
<input type=”hidden” name=”MovieID” value=”<%=mb.getId() %>”/>
<input type=”submit” value=”Delete”/></td></form>
<form action =”Update2.jsp” method =”get”>
<td>
<input type=”hidden” name=”MovieID” value=”<%=mb.getId() %>”/>
<input type=”submit” Value=”Update” /></td></form>
</tr>
<%// continue scriptlet
} // end while
}
%><%–end scriptlet –%>
</tbody>
</table>
<p><a href =”Start.jsp”  >Please click to return Main screen!!!</a></p>
</td>
<td width=”20%”>
<jsp:include page =”Links.html” flush =”true”/>
</td>
</tr>
</tbody>
</table>
</body>
</html><!–end XHTML document –>
Update.jsp
<!DOCTYPE HTML PUBLIC “-//W3C//DTD HTML 4.0 Transitional//EN”>
<%@page import =”java.util.Enumeration”%>
<%@page import =”ymlab.proje1.servlets.*”%>
<html>
<head>
<title>Update JSP</title>
<link rel=STYLESHEET
href=”./extra/jsp.css”
type=”text/css”>
</head>
<body>
<table  border=”3px” cellpadding=”0″ cellspacing=”0″ width=”100%”>
<tbody>
<tr>
<td colspan=”3″ width=”100%”>
<p>Movie Update</p>
</td>
</tr>
<tr>
<td width=”20%”>
<jsp:include page =”sinemalar.html” flush =”true”/>
</td>
<td width=”60%” >
<form action =”Update1″ method =”get”>
<p>Please enter the movie id:</p>
<table border= “3px groove” align=”center” >
<tr>
<td> Movie Id </td>
<td>
<input type =”text” name =”MovieID” />
</td>
</tr>
<tr>
<td colspan=”2″>
<input type =”submit” value =”Submit”/>
</td>
</tr>
</table>
</form>
</td>
<td width=”20%”>
<jsp:include page =”Links.html” flush =”true”/>
</td>
</tr>
</tbody>
</table>
</body>
</html><!–end XHTML document –>
Update2.jsp
<!DOCTYPE HTML PUBLIC “-//W3C//DTD HTML 4.0 Transitional//EN”>
<html>
<%@page import =”java.util.*”%>
<%@page import =”ymlab.proje1.servlets.*”%>
<head>
<title>Update Page JSP</title>
<link rel=STYLESHEET
href=”./extra/jsp.css”
type=”text/css”>
</head>
<body>
<table  border=”3px” cellpadding=”0″ cellspacing=”0″ width=”100%”>
<tbody>
<tr>
<td colspan=”3″ width=”100%”>
<p>Movie Update</p>
</td>
</tr>
<tr>
<td width=”20%”>
<jsp:include page =”sinemalar.html” flush =”true”/>
</td>
<td width=”60%” >
<% MovieBean mb = (MovieBean)(session.getAttribute(request.getParameter(“MovieID”))) ; %>
<form action =”Update2″ method =”get”>
<p>Please type the movie features that you want to edit:</p>
<table border= “3px groove” align=”center” >
<tr>
<td style =”width: 150px;”  class=”TEXT”>Movie ID</td>
<td><%=mb.getId()%></td>
<td><input type=”hidden” name=”MovieID” value=”<%=mb.getId() %>”/></td>
</tr>
<tr>
<td style =”width: 150px;”  class=”TEXT”> Movie Name</td>
<td><%=mb.getName()%></td>
<td><input type=”text” name=”MovieName”/></td>
</tr>
<tr>
<td style =”width: 150px;”  class=”TEXT”> Movie Category</td>
<td> <%=mb.getCategory()%></td>
<td><input type=”text” name=”MovieCategory”/></td>
</tr>
<tr>
<td colspan =”3″>
<input type =”submit” value =”Submit”/>
</td>
</tr>
</table>
</form>
</td>
<td width=”20%”>
<jsp:include page =”Links.html” flush =”true”/>
</td>
</tr>
</tbody>
</table>
</form>
</body>
</html>
Hatalı durumlar için bazı hatalı durum yönlendirmeleri de bulunmaktadır:
ErrorAddedBefore.jsp
<!DOCTYPE HTML PUBLIC “-//W3C//DTD HTML 4.0 Transitional//EN”>
<%@page import =”java.util.*”%>
<%@page import =”ymlab.proje1.servlets.*”%>
<html>
<head>
<title>Movie Addition</title>
<link rel=STYLESHEET
href=”./extra/jsp.css”
type=”text/css”>
</head>
<body>
<table  border=”3px” cellpadding=”0″ cellspacing=”0″ width=”100%”>
<tbody>
<tr>
<td colspan=”3″ width=”100%”>
<p>Movie Addition</p>
</td>
</tr>
<tr>
<td width=”20%”>
<%@include file =”/sinemalar.html” %>
</td>
<td width=”60%” >
<p>This id is added before!!</p>
<% MovieBean mb = (MovieBean)(session.getAttribute(request.getParameter(“MovieID”))) ; %>
<table border= “3px groove” align=”center” >
<tr>
<td style =”width: 200px;”  class=”TEXT”>Movie ID</td>
<td><%=mb.getId()%></td>
</tr>
<tr>
<td style =”width: 200px;”  class=”TEXT”> Movie Name</td>
<td><%=mb.getName()%></td>
</tr>
<tr>
<td style =”width: 200px;”  class=”TEXT”> Movie Category</td>
<td> <%=mb.getCategory()%></td>
</tr>
</table>
<p><a href =”Add.jsp”>Please click here to return Add screen!!!</a></p>
<p><a href =”Start.jsp”>Please click here to return Main screen!!!</a></p>
</td>
<td width=”20%”>
<%@include file =”/Links.html” %>
</td>
</tr>
</tbody>
</table>
</body>
</html>
ErrorEmptyList.jsp
<!DOCTYPE HTML PUBLIC “-//W3C//DTD HTML 4.0 Transitional//EN”>
<html>
<head>
<title>Error Empty List Page</title>
<link rel=STYLESHEET
href=”./extra/jsp.css”
type=”text/css”>
</head>
<body>
<table  border=”3px” cellpadding=”0″ cellspacing=”0″ width=”100%”>
<tbody>
<tr>
<td colspan=”3″ width=”100%”>
<p> Error Empty List Page</p>
</td>
</tr>
<tr>
<td width=”20%”>
<%@include file =”/sinemalar.html” %>
</td>
<td width=”60%” >
<p>There is no movie in the list!!</p> <br><br><br>
<p><a href =”Start.jsp”>Please click here to return main screen!!!</a></p>
</td>
<td width=”20%”>
<%@include file =”/Links.html” %>
</td>
</tr>
</tbody>
</table>
</body>
</html>
ErrorFormAdd.jsp (Film eklerken alanlardan bir ya da birkaçı eksik girilirse eksik girilen alan ya da alanlar uyarılır..)
<!DOCTYPE HTML PUBLIC “-//W3C//DTD HTML 4.0 Transitional//EN”>
<html>
<head>
<title>Error Form Page</title>
<link rel=STYLESHEET
href=”./extra/jsp.css”
type=”text/css”>
</head>
<body>
<table  border=”3px” cellpadding=”0″ cellspacing=”0″ width=”100%”>
<tbody>
<tr>
<td colspan=”3″ width=”100%”>
<p>Error Form Page</p>
</td>
</tr>
<tr>
<td width=”20%”>
<%@include file =”/sinemalar.html” %>
</td>
<td width=”60%” >
<%
String ID   = request.getParameter(“MovieID”);
%>
<p class= message>Please fill
<% if (ID.trim().length() == 0) {
%>
movie ID
<%
}String name = request.getParameter(“MovieName”);
if(name.trim().length() == 0) {
%>
movie name
<%
}
String category = request.getParameter(“MovieCategory”);
if(category.trim().length() == 0){
%>
movie category
<%} %>
! </p><br><br><br>
<p><a href =”Add.jsp”>Please click here to return Add screen!!!</a></p>
</td>
<td width=”20%”>
<%@include file =”/Links.html” %>
</td>
</tr>
</tbody>
</table>
</body>
</html>
ErrorFormDelete.jsp (Silerken Id girilmezse)
<!DOCTYPE HTML PUBLIC “-//W3C//DTD HTML 4.0 Transitional//EN”>
<html>
<head>
<title>Error Form Page</title>
<link rel=STYLESHEET
href=”./extra/jsp.css”
type=”text/css”>
</head>
<body>
<table  border=”3px” cellpadding=”0″ cellspacing=”0″ width=”100%”>
<tbody>
<tr>
<td colspan=”3″ width=”100%”>
<p>Error Form Page</p>
</td>
</tr>
<tr>
<td width=”20%”>
<%@include file =”/sinemalar.html” %>
</td>
<td width=”60%” >
<%
String ID   = request.getParameter(“MovieID”);
%>
<p>Please fill
<% if (ID.trim().length() == 0) {
%>
movie ID
<% }%>
! </p><br><br><br>
<p><a href =”Delete.jsp”>Please click here to return Delete screen!!!</a></p>
</td>
<td width=”20%”>
<%@include file =”/Links.html” %>
</td>
</tr>
</tbody>
</table>
</body>
</html>
ErrorFormUpdate.jsp (Id girilerek Update edileceği seçimde Id girilmeden submit edilmeye çalışıldığı durumda)
<!DOCTYPE HTML PUBLIC “-//W3C//DTD HTML 4.0 Transitional//EN”>
<html>
<head>
<title>Error Form Page</title>
<link rel=STYLESHEET
href=”./extra/jsp.css”
type=”text/css”>
</head>
<body>
<table  border=”3px” cellpadding=”0″ cellspacing=”0″ width=”100%”>
<tbody>
<tr>
<td colspan=”3″ width=”100%”>
<p>Error Form Page</p>
</td>
</tr>
<tr>
<td width=”20%”>
<%@include file =”/sinemalar.html” %>
</td>
<td width=”60%” >
<%
String ID   = request.getParameter(“MovieID”);
%>
<p>Please fill
<% if (ID.trim().length() == 0) {
%>
movie ID
<% }%>
! </p><br><br><br>
<p><a href =”Update.jsp”>Please click here to return Update screen!!!</a></p>
</td>
<td width=”20%”>
<%@include file =”/Links.html” %>
</td>
</tr>
</tbody>
</table>
</body>
</html>
NoUpdate.jsp (Update’de alanlar boş bırakılıp update seçildiyse)
<!DOCTYPE HTML PUBLIC “-//W3C//DTD HTML 4.0 Transitional//EN”>
<%@page import =”java.util.*”%>
<%@page import =”ymlab.proje1.servlets.*”%>
<html>
<head>
<title>No Update</title>
<link rel=STYLESHEET
href=”./extra/jsp.css”
type=”text/css”>
</head>
<body>
<table  border=”3px” cellpadding=”0″ cellspacing=”0″ width=”100%”>
<tbody>
<tr>
<td colspan=”3″ width=”100%”>
<p>No Update Page</p>
</td>
</tr>
<tr>
<td width=”20%”>
<%@include file =”/sinemalar.html” %>
</td>
<td width=”60%” >
<% MovieBean mb = (MovieBean)(session.getAttribute(request.getParameter(“MovieID”))) ;%>
<p>There would be done no update!!</p>
<table border= “3px groove” align=”center” >
<tr>
<td style =”width: 200px;”  class=”TEXT”>Movie ID</td>
<td><%=mb.getId()%></td>
</tr>
<tr>
<td style =”width: 200px;”  class=”TEXT”> Movie Name</td>
<td><%=mb.getName()%></td>
</tr>
<tr>
<td style =”width: 200px;”  class=”TEXT”> Movie Category</td>
<td> <%=mb.getCategory()%></td>
</tr>
</table>
<p><a href =”Update.jsp”>Please click here to return Update screen!!!</a></p>
<p><a href =”Start.jsp”>Please click here to return Main screen!!!</a></p>
</td>
<td width=”20%”>
<%@include file =”/Links.html” %>
</td>
</tr>
</tbody>
</table>
</body>
</html>
WrongIdDelete.jsp
<!DOCTYPE HTML PUBLIC “-//W3C//DTD HTML 4.0 Transitional//EN”>
<%@page import =”java.util.*”%>
<%@page import =”ymlab.proje1.servlets.*”%>
<html>
<head>
<title>Wrong ID Page</title>
<link rel=STYLESHEET
href=”./extra/jsp.css”
type=”text/css”>
</head>
<body>
<table  border=”3px” cellpadding=”0″ cellspacing=”0″ width=”100%”>
<tbody>
<tr>
<td colspan=”3″ width=”100%”>
<p>Wrong ID Page</p>
</td>
</tr>
<tr>
<td width=”20%”>
<%@include file =”/sinemalar.html” %>
</td>
<td width=”60%” >
<p>There is no such movie with the ID :
<%=request.getParameter(“MovieID”) %><br />
<p><a href =”Delete.jsp”>Please click here to return Delete screen!!!</a></p>
<p><a href =”Start.jsp”>Please click here to return Main screen!!!</a></p>
</td>
<td width=”20%”>
<%@include file =”/Links.html” %>
</td>
</tr>
</tbody>
</table>
</body>
</html>
WrongIdUpdate.jsp
<!DOCTYPE HTML PUBLIC “-//W3C//DTD HTML 4.0 Transitional//EN”>
<%@page import =”java.util.*”%>
<%@page import =”ymlab.proje1.servlets.*”%>
<html>
<head>
<title>Wrong ID Page</title>
<link rel=STYLESHEET
href=”./extra/jsp.css”
type=”text/css”>
</head>
<body>
<table  border=”3px” cellpadding=”0″ cellspacing=”0″ width=”100%”>
<tbody>
<tr>
<td colspan=”3″ width=”100%”>
<p>Wrong ID Page</p>
</td>
</tr>
<tr>
<td width=”20%”>
<%@include file =”/sinemalar.html” %>
</td>
<td width=”60%” >
<p>There is no such movie with the ID :
<%=request.getParameter(“MovieID”) %><br />
<p><a href =”Update.jsp”>Please click here to return Update screen!!!</a></p>
<p><a href =”Start.jsp”>Please click here to return Main screen!!!</a></p>
</td>
<td width=”20%”>
<%@include file =”/Links.html” %>
</td>
</tr>
</tbody>
</table>
</body>
</html>
Sonucun başarılı olması durumunda gösterilecek olan jsp sayfaları:
Added.jsp
<!DOCTYPE HTML PUBLIC “-//W3C//DTD HTML 4.0 Transitional//EN”>
<%@page import =”java.util.*”%>
<%@page import =”ymlab.proje1.servlets.*”%>
<html>
<head>
<title>Movie Addition</title>
<link rel=STYLESHEET
href=”./extra/jsp.css”
type=”text/css”>
</head>
<body>
<table  border=”3px” cellpadding=”0″ cellspacing=”0″ width=”100%”>
<tbody>
<tr>
<td colspan=”3″ width=”100%”>
<p>Movie Addition</p>
</td>
</tr>
<tr>
<td width=”20%”>
<%@include file=”/sinemalar.html” %>
</td>
<td width=”60%” >
<% MovieBean mb = (MovieBean)(session.getAttribute(request.getParameter(“MovieID”))) ;%>
<p>Movie added successfully!!</p>
<table border= “3px groove” align=”center” >
<tr>
<td style =”width: 200px;”  class=”TEXT”>Movie ID</td>
<td><%=mb.getId()%></td>
</tr>
<tr>
<td style =”width: 200px;”  class=”TEXT”> Movie Name</td>
<td><%=mb.getName()%></td>
</tr>
<tr>
<td style =”width: 200px;”  class=”TEXT”> Movie Category</td>
<td> <%=mb.getCategory()%></td>
</tr>
</table>
<p><a href =”Add.jsp”>Please click here to add another movie!!!</a></p>
<p><a href =”Start.jsp”>Please click here to return Main screen!!!</a></p>
</td>
<td width=”20%”>
<%@include file =”/Links.html” %>
</td>
</tr>
</tbody>
</table>
</body>
</html>
Deleted.jsp
<!DOCTYPE HTML PUBLIC “-//W3C//DTD HTML 4.0 Transitional//EN”>
<%@page import =”java.util.*”%>
<%@page import =”ymlab.proje1.servlets.*”%>
<html>
<head>
<title>Movie Deletion</title>
<link rel=STYLESHEET
href=”./extra/jsp.css”
type=”text/css”>
</head>
<body>
<table  border=”3px” cellpadding=”0″ cellspacing=”0″ width=”100%”>
<tbody>
<tr>
<td colspan=”3″ width=”100%”>
<p>Movie Deletion</p>
</td>
</tr>
<tr>
<td width=”20%”>
<%@include file =”/sinemalar.html” %>
</td>
<td width=”60%” >
<% MovieBean mb = (MovieBean)(session.getAttribute(request.getParameter(“MovieID”))) ;
session.removeAttribute(request.getParameter(“MovieID”));%>
<p>The following movie deleted successfully!!</p>
<table border= “3px groove” align=”center” >
<tr>
<td style =”width: 200px;”  class=”TEXT”>Movie ID</td>
<td><%=mb.getId()%></td>
</tr>
<tr>
<td style =”width: 200px;”  class=”TEXT”> Movie Name</td>
<td><%=mb.getName()%></td>
</tr>
<tr>
<td style =”width: 200px;”  class=”TEXT”> Movie Category</td>
<td> <%=mb.getCategory()%></td>
</tr>
</table>
<p><a href =”Delete.jsp”>Please click here to delete another movie!!!</a></p>
<p><a href =”Start.jsp”>Please click here to return Main screen!!!</a></p>
</td>
<td width=”20%”>
<%@include file =”/Links.html” %>
</td>
</tr>
</tbody>
</table>
</body>
</html>
Updated.jsp
<!DOCTYPE HTML PUBLIC “-//W3C//DTD HTML 4.0 Transitional//EN”>
<%@page import =”java.util.*”%>
<%@page import =”ymlab.proje1.servlets.*”%>
<html>
<head>
<title>Movie Update</title>
<link rel=STYLESHEET
href=”./extra/jsp.css”
type=”text/css”>
</head>
<body>
<table  border=”3px” cellpadding=”0″ cellspacing=”0″ width=”100%”>
<tbody>
<tr>
<td colspan=”3″ width=”100%”>
<p>Movie Update</p>
</td>
</tr>
<tr>
<td width=”20%”>
<%@include file =”/sinemalar.html”%>
</td>
<td width=”60%” >
<% MovieBean mb = (MovieBean)(session.getAttribute(request.getParameter(“MovieID”))) ;%>
<p>Movie updated successfully!!</p>
<table border= “3px groove” align=”center” >
<tr>
<td style =”width: 200px;”  class=”TEXT”>Movie ID</td>
<td><%=mb.getId()%></td>
</tr>
<tr>
<td style =”width: 200px;”  class=”TEXT”> Movie Name</td>
<td><%=mb.getName()%></td>
</tr>
<tr>
<td style =”width: 200px;”  class=”TEXT”> Movie Category</td>
<td> <%=mb.getCategory()%></td>
</tr>
</table>
<p><a href =”Update.jsp”>Please click here to update another movie!!!</a></p>
<p><a href =”Start.jsp”>Please click to return Main screen!!!</a></p>
</td>
<td width=”20%”>
<%@include file =”/Links.html” %>
</td>
</tr>
</tbody>
</table>
</body>
</html>
Servletler
Add.java
package ymlab.proje1.servlets;
import java.io.IOException;
import java.util.Enumeration;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
/**
* Servlet implementation class Add
*/
public class Add extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public Add() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
String Id = (request.getParameter(“MovieID”)).toString();
String Name = (request.getParameter(“MovieName”)).toString();
String Category = (request.getParameter(“MovieCategory”)).toString();
if(Id.trim().equals(“”) || Name.trim().equals(“”) || Category.trim().equals(“”)){
String address = “/WEB-INF/ErrorPages/ErrorFormAdd.jsp”;
RequestDispatcher dispatcher =request.getRequestDispatcher(address);
dispatcher.forward(request, response);
}
else{
// id nin olup olmadıı session gezilerek kontrol edilmeli
HttpSession session = request.getSession( false);
// get names of session object’s values
Enumeration valueNames;
if( session != null)
valueNames = session.getAttributeNames();
else
valueNames = null;
if( valueNames != null&&
valueNames.hasMoreElements() ) {
String ID;
MovieBean value = new MovieBean(“”,””,””);
while( valueNames.hasMoreElements() ) {
ID = valueNames.nextElement().toString();
value = (MovieBean)session.getAttribute( ID );
if(ID.equals(Id)){
String address = “/WEB-INF/ErrorPages/ErrorAddedBefore.jsp”;
RequestDispatcher dispatcher =request.getRequestDispatcher(address);
dispatcher.forward(request, response);
break;
}
MovieBean movieBean = new MovieBean(Id,
Name,
Category);
session.setAttribute(Id, movieBean);
String address = “/WEB-INF/SuccessfullResultScreens/Added.jsp”;
RequestDispatcher dispatcher =request.getRequestDispatcher(address);
dispatcher.forward(request, response);
}
}
else{
MovieBean movieBean = new MovieBean(Id,
Name,
Category);
session.setAttribute(Id, movieBean);
String address = “/WEB-INF/SuccessfullResultScreens/Added.jsp”;
RequestDispatcher dispatcher =request.getRequestDispatcher(address);
dispatcher.forward(request, response);
}
}
}
}
Delete.java
package ymlab.proje1.servlets;
import java.io.IOException;
import java.util.Enumeration;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
/**
* Servlet implementation class Delete
*/
public class Delete extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public Delete() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
String Id = (request.getParameter(“MovieID”).toString());
if(Id.trim().equals(“”)){
String address = “/WEB-INF/ErrorPages/ErrorFormDelete.jsp”;
RequestDispatcher dispatcher =request.getRequestDispatcher(address);
dispatcher.forward(request, response);
}
else{
HttpSession session = request.getSession( false);
// get names of session object’s values
Enumeration valueNames;
if( session != null)
valueNames = session.getAttributeNames();
else
valueNames = null;
if( valueNames != null&&
valueNames.hasMoreElements() ) {
String ID;
MovieBean value = new MovieBean(“”,””,””);
while( valueNames.hasMoreElements() ) {
ID = valueNames.nextElement().toString();
value = (MovieBean)session.getAttribute( ID );
if(ID.equals(Id)){
String address = “/WEB-INF/SuccessfullResultScreens/Deleted.jsp”;
RequestDispatcher dispatcher =request.getRequestDispatcher(address);
dispatcher.forward(request, response);
break;
}
String address = “/WEB-INF/ErrorPages/WrongIdDelete.jsp”;
RequestDispatcher dispatcher =request.getRequestDispatcher(address);
dispatcher.forward(request, response);
}
}
else{
String address = “/WEB-INF/ErrorPages/ErrorEmptyList.jsp”;
RequestDispatcher dispatcher =request.getRequestDispatcher(address);
dispatcher.forward(request, response);
}
}
}
}
Update1.java
package ymlab.proje1.servlets;
import java.io.IOException;
import java.util.Enumeration;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
/**
* Servlet implementation class Update1
*/
public class Update1 extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public Update1() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
String Id = request.getParameter(“MovieID”);
HttpSession session = request.getSession(false);
if(Id.trim().equals(“”)){
String address = “/WEB-INF/ErrorPages/ErrorFormUpdate.jsp”;
RequestDispatcher dispatcher =request.getRequestDispatcher(address);
dispatcher.forward(request, response);
}
else{
// get names of session object’s values
Enumeration valueNames;
if( session != null)
valueNames = session.getAttributeNames();
else
valueNames = null;
if( valueNames != null&&
valueNames.hasMoreElements() ) {
String ID;
MovieBean value = new MovieBean(“”,””,””);
while( valueNames.hasMoreElements() ) {
ID = valueNames.nextElement().toString();
value = (MovieBean)session.getAttribute( ID );
if(ID.equals(Id)){
String address = “Update2.jsp”;
RequestDispatcher dispatcher =request.getRequestDispatcher(address);
dispatcher.forward(request, response);
break;
}
String address = “/WEB-INF/ErrorPages/WrongIdUpdate.jsp”;
RequestDispatcher dispatcher =request.getRequestDispatcher(address);
dispatcher.forward(request, response);
}
}
else{
String address = “/WEB-INF/ErrorPages/ErrorEmptyList.jsp”;
RequestDispatcher dispatcher =request.getRequestDispatcher(address);
dispatcher.forward(request, response);
}
}
}
}
Update2.java
package ymlab.proje1.servlets;
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;
import javax.servlet.http.HttpSession;
/**
* Servlet implementation class Update
*/
public class Update2 extends HttpServlet {
private static final long serialVersionUID = 1L;
private boolean control1=false;
private boolean control2=false;
/**
* @see HttpServlet#HttpServlet()
*/
public Update2() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
String ID = request.getParameter(“MovieID”);
String Name = request.getParameter(“MovieName”);
String Category = request.getParameter(“MovieCategory”);
HttpSession session = request.getSession(false);
MovieBean mb = (MovieBean)session.getAttribute(ID);
control1=false;
control2=false;
if(!Name.trim().equals(“”))
control1 = true;
if(!Category.trim().equals(“”))
control2 = true;
if(control1==true && control2==true){
session.removeAttribute(ID);
mb = new MovieBean(ID,Name,Category);
session.setAttribute(ID, mb);
String address = “/WEB-INF/SuccessfullResultScreens/Updated.jsp”;
RequestDispatcher dispatcher =request.getRequestDispatcher(address);
dispatcher.forward(request, response);
}
else if(control1==true && control2==false){
session.removeAttribute(ID);
mb = new MovieBean(ID,Name,mb.getCategory());
session.setAttribute(ID, mb);
String address = “/WEB-INF/SuccessfullResultScreens/Updated.jsp”;
RequestDispatcher dispatcher =request.getRequestDispatcher(address);
dispatcher.forward(request, response);
}
else if(control1==false && control2==true){
session.removeAttribute(ID);
mb = new MovieBean(ID,mb.getName(),Category);
session.setAttribute(ID, mb);
String address = “/WEB-INF/SuccessfullResultScreens/Updated.jsp”;
RequestDispatcher dispatcher =request.getRequestDispatcher(address);
dispatcher.forward(request, response);
}
else{
String address = “/WEB-INF/ErrorPages/NoUpdate.jsp”;
RequestDispatcher dispatcher =request.getRequestDispatcher(address);
dispatcher.forward(request, response);
}
}
}
MovieBean.java

package ymlab.proje1.servlets;
public class MovieBean {
private final String id, name, category;
public String getId() {
return id;
}
public String getName() {
return name;
}
public String getCategory() {
return category;
}
public MovieBean(String id, String name, String category) {
super();
this.id = id;
this.name = name;
this.category = category;
}
}

MovieControl.java
package ymlab.proje1.servlets;
import java.util.*;
public class MovieControl {
private static Map <String, MovieBean> movieBeans;
public static Map<String, MovieBean> getMovies() {
return movieBeans;
}
public static MovieBean getMovie(String id){
return movieBeans.get(id);
}
}
web.xml
<?xml version=”1.0″ encoding=”UTF-8″?>
<display-name>20624395</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<servlet>
<description></description>
<display-name>MenuSelector</display-name>
<servlet-name>MenuSelector</servlet-name>
<servlet-class>ymlab.proje1.servlets.MenuSelector</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>MenuSelector</servlet-name>
<url-pattern>/MenuSelector</url-pattern>
</servlet-mapping>
<servlet>
<description></description>
<display-name>Add</display-name>
<servlet-name>Add</servlet-name>
<servlet-class>ymlab.proje1.servlets.Add</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Add</servlet-name>
<url-pattern>/Add</url-pattern>
</servlet-mapping>
<servlet>
<description></description>
<display-name>Delete</display-name>
<servlet-name>Delete</servlet-name>
<servlet-class>ymlab.proje1.servlets.Delete</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Delete</servlet-name>
<url-pattern>/Delete</url-pattern>
</servlet-mapping>
<servlet>
<description></description>
<display-name>Update</display-name>
<servlet-name>Update</servlet-name>
<servlet-class>ymlab.proje1.servlets.Update</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Update</servlet-name>
<url-pattern>/Update</url-pattern>
</servlet-mapping>
<servlet>
<description></description>
<display-name>Update1</display-name>
<servlet-name>Update1</servlet-name>
<servlet-class>ymlab.proje1.servlets.Update1</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Update1</servlet-name>
<url-pattern>/Update1</url-pattern>
</servlet-mapping>
<servlet>
<description></description>
<display-name>Update2</display-name>
<servlet-name>Update2</servlet-name>
<servlet-class>ymlab.proje1.servlets.Update2</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Update2</servlet-name>
<url-pattern>/Update2</url-pattern>
</servlet-mapping>
</web-app>
Reklamlar