Source Code Materi III Daur Hidup Servlet dan Membuat Objek Servlet
Source Code Materi III Daur Hidup Servlet dan Membuat Objek Servlet
protected
void processRequest(HttpServletRequest request, HttpServletResponse response)
throwsServletException,
IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter
out = response.getWriter();
try {
///* TODO output your page here
out.println("<html>");
out.println("<head>");
out.println("<title>Servlet
HaloServlet</title>");
out.println("</head>");
out.println("<body>");
out.println("<h1>Servlet
HaloServlet at " + request.getContextPath () + "</h1>");
out.println("</body>");
out.println("</html>");
//*/
}
finally
{
out.close();
}
<br/>
<a href="./halo">Halo</a>
<?xml version="1.0"
encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<servlet>
<servlet-name>HaloServlet</servlet-name>
<servlet-class>servlets.HaloServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>HaloServlet</servlet-name>
<url-pattern>/halo</url-pattern>
</servlet-mapping>
</web-app>
<!DOCTYPE
HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title></title>
</head>
<body>
<form
action="parameter" method="POST">
Nama
= <input type="text" name="username" value=""
/>
<input
type="submit" value="Send" name="send" />
</form>
</body>
</html>
protected
void doPost(HttpServletRequest request, HttpServletResponse response)
throwsServletException,
IOException {
// ambilmasukandari user
String userName =
request.getParameter("username");
//
ambilobjekPrintWriterdangunakanuntukmenampilkanpesan
response.setContentType("text/html;charset=UTF-8");
PrintWriter
out = response.getWriter();
out.println("<html>");
out.println("<head><title>GetParameterServlet</title></head>");
out.println("<body>");
out.println("<h1>");
out.println("Halo
" + userName + ", Apa
kabar ? ");
out.println("</h1>");
out.println("</body>");
out.println("</html>");
out.close();
}
<servlet>
<servlet-name>HaloServlet</servlet-name>
<servlet-class>servlets.HaloServlet</servlet-class>
</servlet>
<servlet>
<servlet-name>GetParameterServlet</servlet-name>
<servlet-class>servlets.GetParameterServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>HaloServlet</servlet-name>
<url-pattern>/halo</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>GetParameterServlet</servlet-name>
<url-pattern>/parameter</url-pattern>
</servlet-mapping>
<br/>
<a
href="./inputdata.html">Input Data</a>
<!DOCTYPE
HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>GetParameterValues
Servlet</title>
</head>
<body>
Pilih olah raga apa saja yang anda inginkan<br />
<form
action="GetParameterValuesServlet" method="POST">
<input
type="checkbox" name="sports" value="Bersepeda"
/>Bersepeda<br/>
<input
type="checkbox" name="sports" value="Bulutangkis"
/>Bulutangkis<br/>
<input
type="checkbox" name="sports" value="Berenang"
/>Berenang<br/>
<input
type="checkbox" name="sports" value="Sepak Bola"
/>Sepak Bola <br/>
<input
type="checkbox" name="sports" value="Lainnya"
/>Lainnya<br/>
<input
type="submit" value="Submit" name="submit" />
</form>
</body>
</html>
protected
void doPost(HttpServletRequest request, HttpServletResponse response)
throwsServletException,
IOException {
String paramValues[] =
request.getParameterValues("sports");
StringBuffermyResponse
= new StringBuffer();
myResponse.append("<html>");
myResponse.append("<head><title>Pilihananda</title></head>");
myResponse.append("<body>");
myResponse.append("<h1>Pilihanandaadalah:
</h1>");
myResponse.append("<ul>");
for
(int i = 0; i <paramValues.length; i++) {
myResponse.append("<li>"+
paramValues[i] +"</li>");
}
myResponse.append("</ul>");
myResponse.append("</body>");
myResponse.append("</html>");
response.setContentType("text/html;charset=UTF-8");
PrintWriter
out = response.getWriter();
out.println(myResponse.toString());
}
<br/>
<a
href="./inputsports.html">Form Sports</a>
0 komentar: