Create a JSP that displays the student name and the current score, given the EMP
ID: 3560971 • Letter: C
Question
Create a JSP that displays the student name and the current score, given the EMPLID and class ID. For each class, a text file is used to store the student name, EMPLID, and current score. The file is named after the class ID with .txt extension. For instance, if the class ID were cmis440, the file name would be cmis440.txt as shown in Figure2. Suppose each line consists of student name, EMPLID, and score. These three items are separated by the # sign. Create an HTML form that enables the user to enter the EMPLID and class ID, as shown in Figure 1(a). Upon clicking the Submit button, the result is displayed, as shown in Figure 1(b). If the EMPLID or the class ID does not match, report an error. Assume three courses are available: CMIS440, CMIS445, and CMIS485.
Figure 1 The HTML form accepts the EMPLID and class ID from the user and sends them to the server to obtain the score.
Figure 2 Example of cmis440.txt file :
John F Smith#111-22-3333#99.5
Explanation / Answer
Accepting Employee id and Class id from User
1) input.jsp
<html>
<body>
<center>
<form action = "./Output.jsp">
Enter Employee Id : <input type = "text" name = "eid" ><br><br>
Enter Class Id : <input type = "text" name = "cid" ><br><br>
<input type = "submit" value = "Display Details" />
</center>
</form>
</body>
</html>
2) Output.jsp
<page import = "java.io.*;" %>
<% String empid,classid;
String studentName;
float currentScore;
%>
<%
try
{
// get the Input Parameters from Input.jsp Page
empid = request.getParameter("eid");
classid = request.getParameter("cid");
// checking that entered classid is matched or NOT
if( classid.equalsIgnoreCase("CMIS440") || classid.equalsIgnoreCase("CMIS445") || classid.equalsIgnoreCase("CMIS485") )
{
FileReader fr=new FileReader(classid+".txt");
BufferedReader br=new BufferedReader(fr);
// reads the Entire Line from Text File
String s = br.readLine();
// splitting the Line with "#"
String arr[] = s.split("#");
// first item is Student Name
studentName = arr[0];
// last ( third ) item is Current Score
currentScore = Float.parseFloat(arr[2]);
}
else
{
%><html>
<body>
<b>File Not Found,,,Please Enter Correct Class Id</b>
</body>
</html><%
}
}
catch(Exception e)
{
e.printStackTrace();
}
%>
<html>
<body bgcolor = "ligth yellow">
<b> <font size = "6" >
Student Name : <%= studentName%> <br>
Current Score : <%= currentScore%>
</font></b>
</body>
</html>