head 1.1; access; symbols IT1FALL00:1.1; locks; strict; comment @# @; 1.1 date 2000.11.10.20.51.03; author dbc; state Exp; branches; next ; desc @@ 1.1 log @Added Files: Counter.java.txt Headers.java.txt HelloUser.java.txt HelloWorld.java.txt InitCounter.java.txt MultiPart.java.txt MultiValue.java.txt Search.java.txt Students.java.txt VendingMachine.java.txt VendingMachine2.java.txt VendingMachine3.java.txt VendingView.java.txt VendingView2.java.txt VendingView3.java.txt headers.html headersEg.html hello.html helloUserEg.html index.html initCounterEg.html multipart.html multipartEg.html multivalue.html multivalueEg.html search.html searchEg.html students.html studentsEg.html vendingmachine2Eg.html vendingmachine3Eg.html vendingmachineEg.html web.xml @ text @ import java.io.*; import java.util.*; import javax.servlet.*; import javax.servlet.http.*; public class Students extends HttpServlet { public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException { resp.setContentType("text/html"); PrintWriter out = resp.getWriter(); out.println(""); Vector students = new Vector() ; String course = parseRequest(students, req) ; out.println("course: " + course + "
") ; out.println("students:
") ; out.println("") ; for(int i = 0 ; i < students.size() ; i++) out.println("") ; out.println("
" + (String) students.get(i) + "
"); out.println(""); } String parseRequest(Vector students, HttpServletRequest req) throws IOException { String course = null ; String contentType = req.getContentType() ; final String bndryKey = "boundary=" ; int bndryOff = contentType.indexOf(bndryKey) ; String boundary = contentType.substring(bndryOff + bndryKey.length()) ; BufferedReader in = new BufferedReader(req.getReader()) ; String line = in.readLine() ; while(true) { // Check boundary of part: if(line == null || line.equals("--" + boundary + "--")) break ; // Read headers within part (find name of parameter): String name = null ; while(true) { String header = in.readLine() ; if(header.length() == 0) break ; final String dispKey = "Content-Disposition:" ; if(dispKey.equalsIgnoreCase(header.substring(0, dispKey.length()))) { final String nameBeginKey = "name=\"" ; final String nameEndKey = "\"" ; int nameOff = header.indexOf(nameBeginKey) + nameBeginKey.length() ; int nameEnd = header.indexOf(nameEndKey, nameOff) ; name = header.substring(nameOff, nameEnd) ; } } // Read data within part if("course".equalsIgnoreCase(name)) { course = in.readLine() ; line = in.readLine() ; } if("students".equalsIgnoreCase(name)) while(true) { line = in.readLine() ; if(line == null || line.startsWith("--" + boundary)) break ; students.addElement(line) ; } } return course ; } } @