Guest User

Untitled

a guest
Jan 1st, 2018
164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.38 KB | None | 0 0
  1. /*
  2. * To change this license header, choose License Headers in Project Properties.
  3. * To change this template file, choose Tools | Templates
  4. * and open the template in the editor.
  5. */
  6. package AndroidControllers;
  7.  
  8. import Database.Authentication;
  9. import java.io.IOException;
  10. import java.io.PrintWriter;
  11. import javax.servlet.ServletException;
  12. import javax.servlet.http.HttpServlet;
  13. import javax.servlet.http.HttpServletRequest;
  14. import javax.servlet.http.HttpServletResponse;
  15. import org.json.simple.JSONObject;
  16.  
  17. /**
  18. *
  19. * @author serge
  20. */
  21. public class Android extends HttpServlet {
  22.  
  23. /**
  24. * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
  25. * methods.
  26. *
  27. * @param request servlet request
  28. * @param response servlet response
  29. * @throws ServletException if a servlet-specific error occurs
  30. * @throws IOException if an I/O error occurs
  31. */
  32. protected void processRequest(HttpServletRequest request, HttpServletResponse response)
  33. throws ServletException, IOException {
  34. response.setContentType("text/html;charset=UTF-8");
  35. try (PrintWriter out = response.getWriter()) {
  36. /* TODO output your page here. You may use following sample code. */
  37. out.println("<!DOCTYPE html>");
  38. out.println("<html>");
  39. out.println("<head>");
  40. out.println("<title>Servlet Android</title>");
  41. out.println("</head>");
  42. out.println("<body>");
  43. out.println("<h1>Servlet Android at " + request.getContextPath() + "</h1>");
  44. out.println("</body>");
  45. out.println("</html>");
  46. }
  47. }
  48.  
  49. // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
  50. /**
  51. * Handles the HTTP <code>GET</code> method.
  52. *
  53. * @param request servlet request
  54. * @param response servlet response
  55. * @throws ServletException if a servlet-specific error occurs
  56. * @throws IOException if an I/O error occurs
  57. */
  58. @Override
  59. protected void doGet(HttpServletRequest request, HttpServletResponse response)
  60. throws ServletException, IOException {
  61. response.setContentType("application/json");
  62. response.setCharacterEncoding("utf-8");
  63. PrintWriter writer = response.getWriter();
  64. String user = request.getParameter("username");
  65. String pass = request.getParameter("password");
  66. PrintWriter out = response.getWriter();
  67. JSONObject logged = Authentication.login_json(user, pass);
  68. if(logged != null)
  69. writer.print(logged);
  70. else{
  71. logged.put("type", "ERROR");
  72. logged.put("username", "ERROR");
  73. writer.print(logged);
  74. }
  75. //processRequest(request, response);
  76. }
  77.  
  78. /**
  79. * Handles the HTTP <code>POST</code> method.
  80. *
  81. * @param request servlet request
  82. * @param response servlet response
  83. * @throws ServletException if a servlet-specific error occurs
  84. * @throws IOException if an I/O error occurs
  85. */
  86. @Override
  87. protected void doPost(HttpServletRequest request, HttpServletResponse response)
  88. throws ServletException, IOException {
  89.  
  90.  
  91. processRequest(request, response);
  92.  
  93. }
  94.  
  95. /**
  96. * Returns a short description of the servlet.
  97. *
  98. * @return a String containing servlet description
  99. */
  100. @Override
  101. public String getServletInfo() {
  102. return "Short description";
  103. }// </editor-fold>
  104.  
  105. }
  106.  
  107. public static JSONObject login_json(String user, String pass){
  108. JSONObject json = new JSONObject();
  109. Connection conn = null;
  110. String sql = "select username, user_type from users where username = '" + user +
  111. "' and password = '" + pass + "';";
  112. try{
  113. Class.forName("com.mysql.jdbc.Driver");
  114. conn = Utilities.connect();
  115. Statement st = conn.createStatement();
  116. ResultSet rs = st.executeQuery(sql);
  117. if(rs.next()){
  118. json.put("username", user);
  119. json.put("type", rs.getString("user_type"));
  120. }
  121. else{
  122. json.put("username", "FAIL");
  123. json.put("type", "FAIL");
  124. }
  125. }
  126. catch(SQLException | ClassNotFoundException ecc){
  127. System.out.println("ERROR: " + ecc.getMessage());
  128. }
  129. finally{
  130. Utilities.disconnect(conn);
  131. }
  132. return json;
  133. }
  134.  
  135. package com.example.serge.biblioteca;
  136. import java.io.BufferedInputStream;
  137. import java.io.BufferedReader;
  138. import java.io.IOException;
  139. import java.io.InputStream;
  140. import java.io.InputStreamReader;
  141. import java.net.HttpURLConnection;
  142. import java.net.MalformedURLException;
  143. import java.net.URL;
  144. import org.json.simple.JSONObject;
  145. import org.json.simple.parser.JSONParser;
  146. import org.json.simple.parser.ParseException;
  147. public class http_authentication{
  148. public static JSONObject login_json(String user, String pass) {
  149. String json_string = "";
  150. JSONObject stream = new JSONObject();
  151. JSONParser parser = new JSONParser();
  152. String url = "http://localhost:43746/Progetto_TWEB/Android?username=" + user
  153. + "&password=" + pass;
  154. try {
  155. URL link = new URL(url);
  156. HttpURLConnection urlConnection = (HttpURLConnection) link.openConnection();
  157.  
  158. // Check the connection status
  159. if (urlConnection.getResponseCode() == 200) {
  160. // if response code = 200 ok
  161. InputStream in = new BufferedInputStream(urlConnection.getInputStream());
  162.  
  163. // Read the BufferedInputStream
  164. BufferedReader r = new BufferedReader(new InputStreamReader(in));
  165. StringBuilder sb = new StringBuilder();
  166. String line;
  167. while ((line = r.readLine()) != null) {
  168. sb.append(line);
  169. }
  170. json_string = sb.toString();
  171. stream = (JSONObject) parser.parse(json_string);
  172.  
  173. // End reading...............
  174. // Disconnect the HttpURLConnection
  175. urlConnection.disconnect();
  176. } else {
  177. // Do something
  178. }
  179. } catch (MalformedURLException e) {
  180. e.printStackTrace();
  181. } catch (IOException e) {
  182. e.printStackTrace();
  183. } catch (ParseException e) {
  184. e.printStackTrace();
  185. } finally {
  186.  
  187. }
  188. // Return the data from specified url
  189. return stream;
  190. }
  191. }
  192.  
  193. package com.example.serge.biblioteca;
  194.  
  195. import android.content.Intent;
  196. import android.os.Bundle;
  197. import android.support.v7.app.AppCompatActivity;
  198. import android.view.View;
  199. import android.widget.Button;
  200. import android.widget.EditText;
  201. import android.widget.TextView;
  202. import org.json.simple.parser.JSONParser;
  203.  
  204. import org.json.simple.JSONObject;
  205. import java.io.BufferedReader;
  206. import java.io.IOException;
  207. import java.io.InputStreamReader;
  208. import java.io.OutputStream;
  209. import java.net.HttpURLConnection;
  210. import java.net.URL;
  211. import java.net.URLConnection;
  212.  
  213. /**
  214. * Created by serge on 28/12/2017.
  215. */
  216.  
  217. public class login extends AppCompatActivity {
  218.  
  219. @Override
  220. protected void onCreate(Bundle savedInstanceState) {
  221. super.onCreate(savedInstanceState);
  222. setContentView(R.layout.activity_login);
  223. final Button login = (Button) findViewById(R.id.login_login);
  224. final EditText username = (EditText) findViewById(R.id.txtUser);
  225. final EditText password = (EditText) findViewById(R.id.txtPass);
  226.  
  227.  
  228. login.setOnClickListener(new View.OnClickListener() {
  229. @Override
  230. public void onClick(View v) {
  231.  
  232. JSONObject logged = new JSONObject();
  233. boolean vuoto = false;
  234. String logged_type = "";
  235. String logged_user = "", user_type = "";
  236. TextView welcome = (TextView)findViewById(R.id.normal_intro);
  237. String user = username.getText().toString();
  238. String pass = password.getText().toString();
  239. try {
  240. logged = http_authentication.login_json(user, pass);
  241. vuoto = logged.isEmpty();
  242. //The following setText always returns an empty String so I think the object is null
  243. username.setText(logged.toString());
  244. }
  245. catch(Exception ecc){
  246. username.setText(ecc.getMessage());
  247. }
  248. }
  249. });
  250. }
  251. }
Add Comment
Please, Sign In to add comment