Skip to content

Instantly share code, notes, and snippets.

@Zeex
Created June 4, 2012 13:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Zeex/2868252 to your computer and use it in GitHub Desktop.
Save Zeex/2868252 to your computer and use it in GitHub Desktop.
ООП
<%@ page import="persons.Person"%>
<%@ page import="java.util.HashMap"%>
<%@ page import="java.util.Map"%>
<%@ page session="true" contentType="text/html;charset=windows-1251"%>
<jsp:useBean id="personList" class="persons.PersonList" scope="application"/>
<html>
<title>Persons!</title>
<body>
<center>
<%
String action = request.getParameter("action");
if (action == null) {
action = "view";
}
if (action.equals("edit") || action.equals("add")) {
Person person = null;
if (action.equals("add")) {
person = new Person();
} else {
String id = request.getParameter("id");
try {
person = personList.get(Integer.parseInt(id));
} catch (Exception e) {
%>Invalid person ID<%
return;
}
}
%>
<form action="index.jsp">
<table border="0">
<tr>
<td align="right">First name</td>
<td><input type="text" name="first_name" value="<%=person.getFirstName()%>"/></td>
</tr>
<tr>
<td align="right">Last name</td>
<td><input type="text" name="last_name" value="<%=person.getLastName()%>"/></td>
</tr>
<tr>
<td align="right">Address</td>
<td><input type="text" name="address" value="<%=person.getAddress()%>"/></td>
</tr>
<tr>
<td align="right">Phone</td>
<td><input type="text" name="phone" value="<%=person.getPhone()%>"/></td>
</tr>
</table>
<input type="submit" value="OK"/>
<%
if (action.equals("add")) {
%>
<input type="hidden" name="action" value="apply_add"/>
<%
} else {
%>
<input type="hidden" name="action" value="apply_edit"/>
<input type="hidden" name="id" value="<%=request.getParameter("id")%>"/>
<%
}
%>
</form>
<%
} else if (action.equals("apply_add")) {
String firstName = request.getParameter("first_name");
if (firstName == null || firstName.isEmpty()) {
%>Missing first name<%
return;
}
String lastName = request.getParameter("last_name");
if (firstName == null || lastName.isEmpty()) {
%>Missing last name<%
return;
}
String address = request.getParameter("address");
if (address == null || address.isEmpty()) {
%>Missing address<%
return;
}
String phone = request.getParameter("phone");
if (phone == null || phone.isEmpty()) {
%>Missing phone<%
return;
}
personList.add(new Person(firstName, lastName, address, phone));
%>
New person added succesfully
<form action="index.jsp">
<input type="hidden" name="action" value="view"/>
<input type="submit" value="Continue"/>
</form>
<%
} else if (action.equals("apply_edit")) {
String id = request.getParameter("id");
try {
Person person = personList.get(Integer.parseInt(id));
String firstName = request.getParameter("first_name");
if (firstName != null) {
person.setFirstName(firstName);
}
String lastName = request.getParameter("last_name");
if (lastName != null) {
person.setLastName(lastName);
}
String address = request.getParameter("address");
if (address != null) {
person.setAddress(address);
}
String phone = request.getParameter("phone");
if (phone != null) {
person.setPhone(phone);
}
%>You edited person <%=id%><%
} catch (Exception e) {
%>Invalid person ID<%
}
%>
<form action="index.jsp">
<input type="hidden" name="action" value="view"/>
<input type="submit" value="Continue"/>
</form>
<%
} else if (action.equals("remove")) {
String id = request.getParameter("id");
try {
personList.remove(Integer.parseInt(id));
%>You removed person <%=id%><%
} catch (Exception e) {
%>Invalid person ID<%
}
%>
<form action="index.jsp">
<input type="hidden" name="action" value="view"/>
<input type="submit" value="Back"/>
</form>
<%
} else {
%>
<table border="1">
<tr>
<th>First Name</th>
<th>Last Name</th>
<th>Address</th>
<th>Phone</th>
<th>Edit?</th>
<th>Remove?</th>
</tr>
<%
for (Map.Entry<Integer, Person> entry : personList.getAll().entrySet()) {
Integer id = entry.getKey();
Person person = entry.getValue();
%>
<tr>
<td><%=person.getFirstName()%></td>
<td><%=person.getLastName()%></td>
<td><%=person.getAddress()%></td>
<td><%=person.getPhone()%></td>
<td>
<a href="index.jsp?action=edit&id=<%=id%>">edit</a>
</td>
<td>
<a href="index.jsp?action=remove&id=<%=id%>">remove</a>
</td>
</tr>
<%
}
%>
</table>
<form action="index.jsp">
<input type="hidden" name="action" value="add"/>
<input type="submit" value="Add"/>
</form>
<%
}
%>
</center>
</body>
</html>
@echo off
if exist "%ORION_JDK_HOME%" (
set JDK_HOME="%ORION_JDK_HOME%"
) else (
set JDK_HOME="%SystemDrive%\Program Files\Java\jdk1.6.0"
if exist "%JDK_HOME%" (
goto found
)
set JDK_HOME="%SystemDrive%\Program Files\Java\jdk1.7.0"
if exist "%JDK_HOME%" (
goto found
)
goto not_found
)
:found
set JDK_HOME=%JDK_HOME%\
set JDK_PATH=%JDK_HOME%\bin\
echo Found JDK installation at %JDK_HOME%
goto launch
:not_found
set JDK_HOME=
set JDK_PATH=
echo Could not find JDK, assuming it's in PATH...
goto launch
:launch
echo Starting Orion server...
%JDK_PATH%java.exe -jar orion.jar
package persons;
public class Person {
private String firstName = "";
private String lastName = "";
private String address = "";
private String phone = "";
public Person() {
}
public Person(String firstName, String lastName, String address, String phone) {
this.firstName = firstName;
this.lastName = lastName;
this.address = address;
this.phone = phone;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
}
package persons;
import persons.Person;
import java.util.HashMap;
import java.util.Map;
public class PersonList {
private int id = 0;
private HashMap<Integer, Person> persons = new HashMap<Integer, Person>();
public PersonList() {
add(new Person("1", "11", "111", "11-11-11"));
add(new Person("2", "22", "222", "22-22-22"));
add(new Person("3", "33", "333", "33-33-33"));
}
public void add(Person person) {
persons.put(id++, person);
}
public Person get(int id) {
return persons.get(id);
}
public final HashMap<Integer, Person> getAll() {
return persons;
}
public void remove(int id) {
persons.remove(id);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment