Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- package AndroidControllers;
- import Database.Authentication;
- import java.io.IOException;
- import java.io.PrintWriter;
- import javax.servlet.ServletException;
- import javax.servlet.http.HttpServlet;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- import org.json.simple.JSONObject;
- /**
- *
- * @author serge
- */
- public class Android extends HttpServlet {
- /**
- * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
- * methods.
- *
- * @param request servlet request
- * @param response servlet response
- * @throws ServletException if a servlet-specific error occurs
- * @throws IOException if an I/O error occurs
- */
- protected void processRequest(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
- response.setContentType("text/html;charset=UTF-8");
- try (PrintWriter out = response.getWriter()) {
- /* TODO output your page here. You may use following sample code. */
- out.println("<!DOCTYPE html>");
- out.println("<html>");
- out.println("<head>");
- out.println("<title>Servlet Android</title>");
- out.println("</head>");
- out.println("<body>");
- out.println("<h1>Servlet Android at " + request.getContextPath() + "</h1>");
- out.println("</body>");
- out.println("</html>");
- }
- }
- // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
- /**
- * Handles the HTTP <code>GET</code> method.
- *
- * @param request servlet request
- * @param response servlet response
- * @throws ServletException if a servlet-specific error occurs
- * @throws IOException if an I/O error occurs
- */
- @Override
- protected void doGet(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
- response.setContentType("application/json");
- response.setCharacterEncoding("utf-8");
- PrintWriter writer = response.getWriter();
- String user = request.getParameter("username");
- String pass = request.getParameter("password");
- PrintWriter out = response.getWriter();
- JSONObject logged = Authentication.login_json(user, pass);
- if(logged != null)
- writer.print(logged);
- else{
- logged.put("type", "ERROR");
- logged.put("username", "ERROR");
- writer.print(logged);
- }
- //processRequest(request, response);
- }
- /**
- * Handles the HTTP <code>POST</code> method.
- *
- * @param request servlet request
- * @param response servlet response
- * @throws ServletException if a servlet-specific error occurs
- * @throws IOException if an I/O error occurs
- */
- @Override
- protected void doPost(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
- processRequest(request, response);
- }
- /**
- * Returns a short description of the servlet.
- *
- * @return a String containing servlet description
- */
- @Override
- public String getServletInfo() {
- return "Short description";
- }// </editor-fold>
- }
- public static JSONObject login_json(String user, String pass){
- JSONObject json = new JSONObject();
- Connection conn = null;
- String sql = "select username, user_type from users where username = '" + user +
- "' and password = '" + pass + "';";
- try{
- Class.forName("com.mysql.jdbc.Driver");
- conn = Utilities.connect();
- Statement st = conn.createStatement();
- ResultSet rs = st.executeQuery(sql);
- if(rs.next()){
- json.put("username", user);
- json.put("type", rs.getString("user_type"));
- }
- else{
- json.put("username", "FAIL");
- json.put("type", "FAIL");
- }
- }
- catch(SQLException | ClassNotFoundException ecc){
- System.out.println("ERROR: " + ecc.getMessage());
- }
- finally{
- Utilities.disconnect(conn);
- }
- return json;
- }
- package com.example.serge.biblioteca;
- import java.io.BufferedInputStream;
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.InputStreamReader;
- import java.net.HttpURLConnection;
- import java.net.MalformedURLException;
- import java.net.URL;
- import org.json.simple.JSONObject;
- import org.json.simple.parser.JSONParser;
- import org.json.simple.parser.ParseException;
- public class http_authentication{
- public static JSONObject login_json(String user, String pass) {
- String json_string = "";
- JSONObject stream = new JSONObject();
- JSONParser parser = new JSONParser();
- String url = "http://localhost:43746/Progetto_TWEB/Android?username=" + user
- + "&password=" + pass;
- try {
- URL link = new URL(url);
- HttpURLConnection urlConnection = (HttpURLConnection) link.openConnection();
- // Check the connection status
- if (urlConnection.getResponseCode() == 200) {
- // if response code = 200 ok
- InputStream in = new BufferedInputStream(urlConnection.getInputStream());
- // Read the BufferedInputStream
- BufferedReader r = new BufferedReader(new InputStreamReader(in));
- StringBuilder sb = new StringBuilder();
- String line;
- while ((line = r.readLine()) != null) {
- sb.append(line);
- }
- json_string = sb.toString();
- stream = (JSONObject) parser.parse(json_string);
- // End reading...............
- // Disconnect the HttpURLConnection
- urlConnection.disconnect();
- } else {
- // Do something
- }
- } catch (MalformedURLException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- } catch (ParseException e) {
- e.printStackTrace();
- } finally {
- }
- // Return the data from specified url
- return stream;
- }
- }
- package com.example.serge.biblioteca;
- import android.content.Intent;
- import android.os.Bundle;
- import android.support.v7.app.AppCompatActivity;
- import android.view.View;
- import android.widget.Button;
- import android.widget.EditText;
- import android.widget.TextView;
- import org.json.simple.parser.JSONParser;
- import org.json.simple.JSONObject;
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.io.OutputStream;
- import java.net.HttpURLConnection;
- import java.net.URL;
- import java.net.URLConnection;
- /**
- * Created by serge on 28/12/2017.
- */
- public class login extends AppCompatActivity {
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_login);
- final Button login = (Button) findViewById(R.id.login_login);
- final EditText username = (EditText) findViewById(R.id.txtUser);
- final EditText password = (EditText) findViewById(R.id.txtPass);
- login.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- JSONObject logged = new JSONObject();
- boolean vuoto = false;
- String logged_type = "";
- String logged_user = "", user_type = "";
- TextView welcome = (TextView)findViewById(R.id.normal_intro);
- String user = username.getText().toString();
- String pass = password.getText().toString();
- try {
- logged = http_authentication.login_json(user, pass);
- vuoto = logged.isEmpty();
- //The following setText always returns an empty String so I think the object is null
- username.setText(logged.toString());
- }
- catch(Exception ecc){
- username.setText(ecc.getMessage());
- }
- }
- });
- }
- }
Add Comment
Please, Sign In to add comment