lab3 client request servlet 1 client servlet
play

LAB3 : Client Request Servlet - PDF document

IT311 Server Side Programming: Lab Information Technology Division LAB3 : Client Request Servlet 1 Client


  1. IT311 Server Side Programming: Lab Information Technology Division LAB3 : การท างานกับ Client Request และ Servlet ตัวอย่างที่ 1 การอ่านข้อมูลจาก Client ร่วมกับ Servlet 1. สร้าง หน้าจอ index.jsp ดังนี้ <%@ page language="java" contentType="text/html; charset=TIS-620" pageEncoding="TIS-620"%> <html> <head><title>itsci web lab4</title></head> <body> <form name="form1" method="Post" action="InputServlet"> <table border=0> <tr> <td colspan=2 align=center><font color=blue size=6> ลงทะเบียนสัมมนาเรื่อง JSP </font><br><br><br></td> </tr> <tr> <td align=right> ชื่อ - นามสกุล : </td> <td align=left> <input name="name" size=25></td> </tr> <tr> <td align=right> สาขาวิชา :</td> <td align=left> <input name="major" size=25> </td> </tr> <tr> <td align=right> วันที่ต ้ องการ :</td> <td align=left><input name="date" size=25></td> </tr> Lecturer: Dr.Sayan Unankard

  2. IT311 Server Side Programming: Lab Information Technology Division <tr> <td align=right> เลือกความถนัด <BR>( เลือกได ้ มากกว่า 1 รายการ ) : </td> <td><input type="checkBox" name="citems" value="HTML">HTML<BR> <input type="checkBox" name="citems" value="JAVA">JAVA<BR> <input type="checkBox" name="citems" value="MySQL">MySql<BR> <input type="checkBox" name="citems" value="JDBC">JDBC </td> </tr> <tr> <td align=right> ประเทศ : </td> <td align=left><select name="country"> <option value="THA">Thailand</option> <option value="JPN">Japan</option> <option value="USA">USA</option> </select></td> </tr> <tr> <td align=right> ยืนยัน : </td> <td align=left><p> <input type="radio" name="items" value="1">Yes<br> <input type="radio" name="items" value="0">No</p></td> </tr> <tr> <td align=right> </td> <td align=left><input type="submit" name="Submit" value=" ตกลง "> <input type="reset" name="Submit2" value=" ยกเลิก "></td> </tr> </table> </form> </body> </html> 2. ก าหนด Form ดังนี้ <form method=" post " action=" InputServlet "> หมายถึงสั่งให้ Server ท างานที่ method post และ ส่งไปยัง InputServlet โดยจะจัดเก็บค่าลงในคลาส Person ดังนี้ package lab3; public class Person { private String name; private String major; private String date; private String[] skill; private String country; public String getName() { return name; } public void setName(String name) { this .name = name; } public String getMajor() { return major; } public void setMajor(String major) { this .major = major; } public String getDate() { return date; } public void setDate(String date) { this .date = date; } public String[] getSkill() { return skill; } public void setSkill(String[] skill) { this .skill = skill; } public String getCountry() { return country; } public void setCountry(String country) { this .country = country; } } Lecturer: Dr.Sayan Unankard

  3. IT311 Server Side Programming: Lab Information Technology Division 3. ส่วนของไฟล์ servlet จะใช้ค าสั่งในการเรียกข้อมูลจาก index.jsp โดยใช้ค าสั่ง ดังนี้ package lab3; import java.io.IOException; import javax.servlet.RequestDispatcher; import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class InputServlet extends javax.servlet.http.HttpServlet implements javax.servlet.Servlet { static final long serialVersionUID = 1L; public InputServlet() { super (); } protected void doPost (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { Person person = new Person(); String name = request.getParameter("name"); String major = request.getParameter("major"); String date = request.getParameter("date"); String[] skill = request.getParameterValues("citems"); String country = request.getParameter("country"); String confirm = request.getParameter("items"); if ("1".equals(confirm)){ person.setName(name); person.setMajor(major); person.setDate(date); person.setSkill(skill); person.setCountry(country); request.setAttribute("person", person); goTo("/result.jsp",request,response); }else{ response.sendRedirect("notConfirm.jsp"); } } // เมธอดส าหรับการส่งค่าไปยังหน้าจอโดยส่งตัวแปรผ่าน request.setAttribute() protected void goTo(String url, HttpServletRequest request, HttpServletResponse response) { if (url != null) { RequestDispatcher dispatcher = getServletContext().getRequestDispatcher(url); try { dispatcher.forward(request, response); } catch (Exception e) {} } } } Lecturer: Dr.Sayan Unankard

  4. IT311 Server Side Programming: Lab Information Technology Division 4. จากนั้น servlet จะส่งค่า request ทั้งหมดไปหน้าจอ result.jsp ดังนี้ <%@ page import="lab3.Person" %> <% Person person = (Person) request.getAttribute("person"); %> <html> <head><title>itsci result lab4</title></head> <body> ชื่อ : <%= person.getName() %><br> สาขาวิชา : <%= person.getMajor() %><br> วันที่ต ้ องการ : <%= person.getDate() %><br> <br> เรียน คุณ <%= person.getName() %>: <br><br> <font color=blue size = 5> ยินดีต ้ อนรับเข ้ าสู่งานสัมมนา <font color=red>' พื้นฐาน JSP'</font></font><br><br> กรุณาพิมพ์เอกสารหน้านี้ พร้อมทั้งลงชื่อเพื่อเข ้ าร่วมสัมมนาในวันที่ <%= person.getDate() %> ด ้ วย <br><br> ความถนัด : <% String[] skill = person.getSkill(); for ( int i=0;i< skill.length;i++) { %> <%= skill[i]%>,&nbsp; <%}%><BR><BR> จากประเทศ : <%= person.getCountry() %><BR><BR> ลงชื่อ ______________ </body> </html> 5. ผลลัพธ์ที่ได้จะแสดงดังหน้าจอต่อไปนี้ Lecturer: Dr.Sayan Unankard

  5. IT311 Server Side Programming: Lab Information Technology Division 6. เช็คเงื่อนไขถ้า เลือก Yes จึงจะแสดงแบบฟอร์ม (ดังรูปข้อ 5) แต่ถ้าตอบ No ให้แสดง ในหน้า notconfirm.jsp ดังรูป ตัวอย่างที่ 2 การจัดการ Attribute ในคลาส Servlet 1. สร้าง Servlet ที่ชื่อว่า CounterServlet ใน Package ที่ชื่อว่า lab3 package lab3; import java.io.IOException; import javax.servlet.RequestDispatcher; import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class CounterServlet extends javax.servlet.http.HttpServlet implements javax.servlet.Servlet { static final long serialVersionUID = 1L; private int count = 0; public CounterServlet() { super (); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { count++; request.setAttribute("count", "" + count); goTo("/index.jsp",request,response); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request,response); } Lecturer: Dr.Sayan Unankard

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend