Guest User

Untitled

a guest
Jan 15th, 2019
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.77 KB | None | 0 0
  1. The server encountered an internal error () that prevented it from fulfilling this request.
  2.  
  3. exception
  4.  
  5. java.lang.NumberFormatException: null
  6. java.lang.Integer.parseInt(Unknown Source)
  7. java.lang.Integer.parseInt(Unknown Source)
  8. CreateLab.doPost(CreateLab.java:72)
  9. javax.servlet.http.HttpServlet.service(HttpServlet.java:641)
  10. javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
  11.  
  12. import java.io.IOException;
  13.  
  14.  
  15. import java.io.PrintWriter;
  16. import java.sql.Connection;
  17. import java.sql.DriverManager;
  18. import java.sql.ResultSet;
  19. import java.sql.SQLException;
  20. import java.sql.Statement;
  21.  
  22. import javax.servlet.ServletException;
  23. import javax.servlet.annotation.WebServlet;
  24. import javax.servlet.http.HttpServlet;
  25. import javax.servlet.http.HttpServletRequest;
  26. import javax.servlet.http.HttpServletResponse;
  27.  
  28. /**
  29. * Servlet implementation class CreateLab
  30. */
  31. @WebServlet("/CreateLab")
  32. public class CreateLab extends HttpServlet {
  33. private static final long serialVersionUID = 1L;
  34.  
  35. /**
  36. * @see HttpServlet#HttpServlet()
  37. */
  38. public CreateLab() {
  39. super();
  40. // TODO Auto-generated constructor stub
  41. }
  42. int id;
  43. int capacity;
  44. String day = "";
  45. String time = "";
  46. String room = "";
  47. int subject_id;
  48. int user_id;
  49.  
  50. public void init() {
  51. try {
  52. Class.forName("com.mysql.jdbc.Driver");
  53. Connection con =
  54. DriverManager.getConnection("jdbc:mysql://localhost:3306/wae","root","");
  55. System.out.println("JDBC driver loaded");
  56. }
  57. catch (ClassNotFoundException e) {
  58. System.out.println(e.toString());
  59. } catch (SQLException e) {
  60. // TODO Auto-generated catch block
  61. e.printStackTrace();
  62. }
  63. }
  64.  
  65. /**Process the HTTP Get request*/
  66. public void doGet(HttpServletRequest request, HttpServletResponse response) throws
  67. ServletException,IOException {
  68. sendPageHeader(response);
  69. sendRegistrationForm(request, response, false);
  70. sendPageFooter(response);
  71. }
  72.  
  73. /**Process the HTTP Post request*/
  74. public void doPost(HttpServletRequest request,
  75. HttpServletResponse response)
  76. throws ServletException, IOException {
  77. sendPageHeader(response);
  78.  
  79. capacity = Integer.parseInt(request.getParameter("capacity"));
  80. id = Integer.parseInt(request.getParameter("id"));
  81. day = request.getParameter("day");
  82. time = request.getParameter("time");
  83. room = request.getParameter("room");
  84. user_id = Integer.parseInt(request.getParameter("user_id"));
  85. subject_id = Integer.parseInt(request.getParameter("subject_id"));
  86.  
  87.  
  88. boolean error = false;
  89. String message = null;
  90. try {
  91. Class.forName("com.mysql.jdbc.Driver");
  92. Connection con =
  93. DriverManager.getConnection("jdbc:mysql://localhost:3306/wae","root","");
  94. System.out.println("got connection");
  95. System.out.println(id);
  96. Statement s = con.createStatement();
  97.  
  98. String sql = "SELECT id FROM user" +
  99. " WHERE id='" + user_id + "'";
  100. ResultSet rs = s.executeQuery(sql);
  101. if (rs.next()) {
  102. rs.close();
  103. sql = "INSERT INTO lab" +
  104. " (capacity, day, time, room, subject_id, user_id)" +
  105. " VALUES" +
  106. " (" + capacity + "','" +
  107. " '" + day + "'," +
  108. " '" + time + "'," +
  109. " '" + room + "','" + subject_id + "','" + user_id + "')";
  110.  
  111. System.out.println(sql);
  112. int i = s.executeUpdate(sql);
  113. if (i==1) {
  114. message = "Successfully a new lab class.";
  115. }
  116. }
  117. s.close();
  118. con.close();
  119. }
  120. catch (SQLException e) {
  121. message = "Error." + e.toString();
  122. error = true;
  123. }
  124. catch (Exception e) {
  125. message = "Error." + e.toString();
  126. error = true;
  127. }
  128. if (message!=null) {
  129. PrintWriter out = response.getWriter();
  130. out.println("<B>" + message + "</B><BR>");
  131. out.println("<HR><BR>");
  132. }
  133. if (error==true)
  134. sendRegistrationForm(request, response, true);
  135. else
  136. sendRegistrationForm(request, response, false);
  137. sendPageFooter(response);
  138. }
  139.  
  140. /**
  141. * Send the HTML page header, including the title
  142. * and the <BODY> tag
  143. */
  144. private void sendPageHeader(HttpServletResponse response)
  145. throws ServletException, IOException {
  146. response.setContentType("text/html");
  147. PrintWriter out = response.getWriter();
  148. out.println("<HTML>");
  149. out.println("<HEAD>");
  150. out.println("<TITLE>Create Lab Page</TITLE>");
  151. out.println("</HEAD>");
  152. out.println("<BODY>");
  153. out.println("<CENTER>");
  154. }
  155.  
  156. /**
  157. * Send the HTML page footer, i.e. the </BODY>
  158. * and the </HTML>
  159. */
  160. private void sendPageFooter(HttpServletResponse response)
  161. throws ServletException, IOException {
  162. PrintWriter out = response.getWriter();
  163. out.println("</CENTER>");
  164. out.println("</BODY>");
  165. out.println("</HTML>");
  166. }
  167. /**Send the form where the user can type in
  168. * the details for a new user
  169. */
  170. private void sendRegistrationForm(HttpServletRequest request,
  171. HttpServletResponse response, boolean displayPreviousValues)
  172. throws ServletException, IOException {
  173.  
  174. PrintWriter out = response.getWriter();
  175. out.println("<BR><H2>Create A Lab</H2>");
  176. out.println("<BR>Please enter the lab details.");
  177. out.println("<BR>");
  178. out.println("<BR><FORM METHOD=POST>");
  179. out.println("<TABLE>");
  180. out.println("<TR>");
  181. out.println("<TD>Class Capacity</TD>");
  182. out.print("<TD><INPUT TYPE=TEXT Name=capacity");
  183.  
  184. if (displayPreviousValues)
  185. out.print(" VALUE="" + capacity + """);
  186.  
  187. out.println("></TD>");
  188. out.println("</TR>");
  189. out.println("<TR>");
  190. out.println("<TD>Day</TD>");
  191. out.print("<TD><INPUT TYPE=TEXT Name=day");
  192.  
  193. if (displayPreviousValues)
  194. out.print(" VALUE="" + day + """);
  195.  
  196. out.println("></TD>");
  197. out.println("</TR>");
  198. out.println("<TR>");
  199. out.println("<TD>Time</TD>");
  200. out.print("<TD><INPUT TYPE=TEXT Name=time");
  201.  
  202. if (displayPreviousValues)
  203. out.print(" VALUE="" + time + """);
  204.  
  205. out.println("></TD>");
  206. out.println("</TR>");
  207. out.println("<TR>");
  208. out.println("<TD>Room</TD>");
  209. out.print("<TD><INPUT TYPE=TEXT Name=room");
  210. if (displayPreviousValues)
  211. out.print(" VALUE="" + room + """);
  212. out.println("></TD>");
  213. out.println("</TR>");
  214. out.println("<TR>");
  215. out.println("<TD>subject_id</TD>");
  216. out.print("<TD><INPUT TYPE=TEXT Name=subject_id");
  217. if (displayPreviousValues)
  218. out.print(" VALUE="" + subject_id + """);
  219. out.println("></TD>");
  220. out.println("</TR>");
  221. out.println("<TR>");
  222. out.println("<TD>user_id</TD>");
  223. out.print("<TD><INPUT TYPE=TEXT Name=user_id");
  224. out.println("></TD>");
  225. out.println("</TR>");
  226.  
  227. if (displayPreviousValues)
  228. out.print(" VALUE="" + user_id + """);
  229. out.println("</TD>");
  230. out.println("</TR>");
  231.  
  232. out.println("<TR>");
  233. out.println("<TD><INPUT TYPE=RESET></TD>");
  234. out.println("<TD><INPUT TYPE=SUBMIT></TD>");
  235. out.println("</TR>");
  236. out.println("</TABLE>");
  237. out.println("</FORM>");
  238. out.println("<BR>");
  239. out.println("<BR>");
  240. }
  241. }
  242.  
  243. public int getIntParameter(String strParamval){
  244.  
  245. int nRetVal=0;
  246. if((strParamval != null) && (!strParamval.matches("\d*")))
  247. return nRetVal;
  248. try{
  249. nRetVal = Integer.parseInt(strParamval);
  250. }catch(NumberFormatException nfe){
  251. }
  252. return nRetVal;
  253. }
  254.  
  255. capacity = getIntParameter(request.getParameter("capacity"));
  256. id = getIntParameter(request.getParameter("id"));
  257. user_id = getIntParameter(request.getParameter("user_id"));
  258. subject_id = getIntParameter(request.getParameter("subject_id"));
  259.  
  260. capacity = Integer.parseInt(request.getParameter("capacity"));
  261. id = Integer.parseInt(request.getParameter("id"));
  262. user_id = Integer.parseInt(request.getParameter("user_id"));
  263. subject_id = Integer.parseInt(request.getParameter("subject_id"));
  264.  
  265. capacity = Integer.parseInt(request.getParameter("capacity"));
  266.  
  267. id = Integer.parseInt(request.getParameter("id"));
  268.  
  269. String str = request.getParameter("capacity");
  270. if(str!=null && str.length > 0){
  271. capacity = Integer.parseInt(str);
  272. }
  273. str = request.getParameter("id");
  274. if(str!=null && str.length > 0){
  275. id = Integer.parseInt(str);
  276. }
  277. str = request.getParameter("user_id");
  278. if(str!=null && str.length > 0){
  279. user_id = Integer.parseInt(str);
  280. }
  281. str = request.getParameter("subject_id");
  282. if(str!=null && str.length > 0){
  283. subject_id = Integer.parseInt(str);
  284. }
  285.  
  286. public int getIntParameter(String strParamval){
  287. int nRetVal=0;
  288. try{
  289. nRetVal=Integer.parseInt(strParamVal);
  290. }catch(NumberFormatException nfe){
  291. return 0;
  292. }
  293. return nRetVal;
  294. }
  295.  
  296. String id =request.getParameter("id");
  297. try {
  298. if(!id.isEmpty()) {
  299. Integer.parseInt(id);
  300. }
  301. }
  302. catch(NFE ex) {
  303. ex.printStrackTrace();
  304. }
  305.  
  306. String id= request.getParameter("id")
  307. try {
  308. if(!id.isEmpty() && id!=null) {
  309. Integer.parseInt();
  310. }
  311. }
  312. catch(NFE ex) {
  313. ex.printStrackTrace();
  314. }
  315.  
  316. user_id = Integer.parseInt(request.getParameter("user_id"));
  317.  
  318. request.getParameter("user_id")
Add Comment
Please, Sign In to add comment