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 ; } }