Note that there are some explanatory texts on larger screens.

plurals
  1. POMalformed URL when connecting applet to servlet
    primarykey
    data
    text
    <p>I'm trying to access a servlet from a java applet and set the servlet's response in the applet's text field. I'm using tomcat 7.0 and my jre/jdk are fully updated. The servlet runs fine (correct output in the browser) when invoked from the browser as localhost:8080/hello/hello?query=select * from airports</p> <p>(where airports is the name of the database)</p> <p>However when i run the applet in appletviewer, i get a Malformed URL exception thrown..</p> <p>Code for Applet:</p> <pre><code>import java.applet.*; import java.awt.*; import java.awt.event.*; import java.net.*; import java.io.*; /* &lt;applet code="lab101" width=500 height=270&gt; &lt;/applet&gt; */ public class lab101 extends Applet implements ActionListener{ TextArea t; Panel p,q,r; CheckboxGroup c; Checkbox ins,dis,del,update; //Checkboxes are included just for testing purposes. TextField f; Label l1; Button b; public void init(){ setLayout(new FlowLayout()); b=new Button("Run"); l1=new Label("Query:"); c=new CheckboxGroup(); t=new TextArea("",10,50); p=new Panel(); q=new Panel(); r=new Panel(); p.add(t); ins=new Checkbox("Insert",c,false); dis=new Checkbox("Display",c,true); del=new Checkbox("Delete",c,false); update=new Checkbox("Update",c,false); f=new TextField(50); q.add(ins); q.add(dis); q.add(del); q.add(update); r.add(l1); r.add(f); r.add(b); b.addActionListener(this); add(p); add(q); add(r); try{ URL url=new URL("127.0.0.1:8080/hello/hello?query=select * from airports"); URLConnection servletconnection=url.openConnection(); servletconnection.setDoInput(true); InputStream in=servletconnection.getInputStream(); String s=""; int ch; loop:while(1&gt;0){ ch=in.read(); if(ch==-1) break loop; else s+=(char)ch; } t.setText(s); }//try close catch(MalformedURLException e){ t.setText("Malformed URL Exception occured.");} catch(IOException e){ t.setText("IO exception occured");} } public void actionPerformed(ActionEvent ae){ } public void start(){ } public void paint(Graphics g){ } }//class ends </code></pre> <p>Code for servlet:</p> <pre><code>import java.io.*; import javax.servlet.ServletException; import javax.servlet.http.*; import java.sql.*; public class hello extends HttpServlet{ public void doGet(HttpServletRequest request,HttpServletResponse response)throws ServletException, IOException { response.setContentType("text/plain"); PrintWriter out=response.getWriter(); String query=request.getParameter("query"); Connection link=null; Statement statement=null; ResultSet results=null; try{ Class.forName("org.postgresql.Driver"); String url = "jdbc:postgresql://localhost:5432/test"; link=DriverManager.getConnection(url,"postgres","hooligan"); out.println("Successful connection"); } catch(ClassNotFoundException e){ out.println("Unable to load driver"); } catch(SQLException e){ out.println("Cannot connect to database"); } try{ statement=link.createStatement(); //String select="select * from airports"; results=statement.executeQuery(query); } catch(SQLException e){ out.println("Cannot execute query"); e.printStackTrace(); } try{ out.println(); while(results.next()){ out.println("Name: " + results.getString(1)); out.println("Location: " + results.getString(2)); //System.out.println("Account no: " + results.getInt(3)); System.out.println();} } catch(SQLException e){ out.println("Error retrieving data"); } try{ link.close();} catch(SQLException e){ out.println("Unable to disconnect");} out.close(); out.flush(); }} </code></pre> <p>Any thoughts?</p> <p>P.S. i also noticed that if i use localhost instead of 127.0.0.1 i get a Security Exception thrown (Probably because the applet is unsigned?)</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload