I'm learning about servlets in java. Bellow is my code that is suppose to get the content of the url, store it in array list and, display it on the screen. for some reason I'm unable to get the string array content to displayed on the screen. When I load the page I get the "no luck" message. Any ideas why? thanks
//package fortune;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.ArrayList;
import java.util.Random;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import static java.util.Arrays.*;
import java.util.Collections;
import java.util.List;
#WebServlet(name = "FortuneServlet", urlPatterns = {"/"})
public class FortuneServlet extends HttpServlet {
//private String [] cookies = null;
List<String> cookies = new ArrayList<>();
String line ;
public void geturl(String[] args) {
try
{
URL url = new URL(" http://fortunes.cat-v.org/openbsd/");
//URL url = new URL(" http://bbc.com");
// read text returned by server
BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
while((line = in.readLine()) != null)
{
cookies.add(line);
//line = in.readLine();
}
in.close();
}
catch (java.net.MalformedURLException e) {
System.out.println("Malformed URL: " + e.getMessage());
}
catch (IOException e) {
System.out.println("I/O Error: " + e.getMessage());
}
}
public void init() throws ServletException {
}
#Override
protected void doGet(
HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
response.setCharacterEncoding("UTF-8");
response.setContentType("text/plain");
if (cookies != null)
{
//response.getWriter().println(
// cookies[new Random().nextInt(cookies.length)]
//);
for (String str: cookies)
{
Collections.shuffle(cookies);
response.getWriter().println(str);
}
}
else {
response.getWriter().println("No luck!");
}
}
}
cookies is always an empty list (unless you are not showing something to us), so you always shuffle it and try to display, but because you don't have anything there you see a blank page.
I would change check cookies != null to !cookies.isEmpty().
EDIT:
You are not adding anything to the cookies list, so it is empty (List<String> cookies = new ArrayList<>();).
Maybe you wanted to call geturl (which does some add on the cookies list) method somewhere in the doGet? Right now it is not used anywhere.
Related
I have a server that handles requests, and with regular intervals (every 1-2 minutes) needs to call another server and update a list of objects that is used to create the response to the request. Inspired by for example the accepted answer for this question, I tried to use the Schedule annotation, but can't get it to work.
How to run a background task in a servlet based web application?
A simplified view of the server is:
import javax.servlet.Servlet;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
#WebServlet(name = "MyServer", urlPatterns = {"/data/*", "/scheduled/*"}, loadOnStartup = 1)
public class MyServlet extends HttpServlet {
private static final String UTF_8 = "UTF-8";
private static final String APPLICATION_JSON = "application/json";
private static final long serialVersionUID = 1L;
private static connector connector;
public MyServlet() {
super();
}
public void destroy() {}
#Override
public void init() throws ServletException {
connector = new connector();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String path = request.getServletPath();
String pathInfo = request.getPathInfo().substring(1);
response.setContentType(APPLICATION_JSON);
response.setCharacterEncoding(UTF_8);
if (path.endsWith("/data")) {
List<DataItem> dataItems = connector.currentData;
response.getWriter().write(JsonUtility.convertToJsonString(dataItems));
} else if (path.endsWith("/scheduled")) {
connector.fetchData();
response.getWriter().write("Done");
} else {
response.getWriter()
.write(pathInfo + " Is not Found but not returning 404");
return;
}
}
}
The connector class that is supposed to update the stored data from the other server at regular intervals is:
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import javax.ejb.Lock;
import javax.ejb.LockType;
import javax.ejb.Schedule;
import javax.ejb.Singleton;
import javax.ejb.Startup;
import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
#Startup
#Singleton
public class Connector {
public List<DataItem> currentData = new LinkedList<>();
#Lock(LockType.READ)
#Schedule(second = "*", minute = "*/2", hour = "*", persistent = false)
public void fetchNews() {
logger.debug("Fetching data");
String response = sendGet(url, bearerToken);
JsonObject json = new Gson().fromJson(response, JsonObject.class);
//Transform the response to a list of DataItems.
List<DataItem> retrievedData = toDataList(json)
System.out.println(retrievedData);
synchronized(currentData) {
currentData = retrievedData;
}
}
private String sendGet(String path, String authorizationProperty) {
URL url;
try {
url = new URL(path);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestProperty("Authorization", authorizationProperty);
conn.setRequestProperty("Content-Type","application/json");
conn.setRequestMethod("GET");
BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream(), StandardCharsets.UTF_8));
String output;
StringBuffer response = new StringBuffer();
while ((output = in.readLine()) != null) {
response.append(output);
}
in.close();
return response.toString();
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
}
However, the method fetchData is not firing every second minute as I was expecting. I don't know what I am missing. I have played around a bit with adding/removing the #Startup annotation, creating an instance of the class in the init() method on the server, but still have no result.
This question already has answers here:
Java URL encoding of query string parameters
(11 answers)
Closed 5 years ago.
I wrote the netbeans ta web service project. But when I write "50% discount" to the value of a parameter I use with http post, it looks like "P discount" to me. How can I fix this problem?
192.168.0.222:7001/Project/KonuEkle?uye=test&&baslik=%50 discount&&mesaj=test&&kategori=123&&link=null
import com.mrkcn.servlet.Classlar.ConnectInfo;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.json.JSONException;
import org.json.JSONObject;
public class KonuEkleServlet extends HttpServlet {
public String kullaniciadi;
public String baslik;
public String mesaj;
public String kategori;
public String altKategori;
public String link;
public Connection con;
boolean action = false;
#Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
try {
req.setCharacterEncoding("UTF-8");
resp.setCharacterEncoding("UTF-8");
ConnectInfo conServlet= new ConnectInfo();
con=null;
con=conServlet.baglanti();
PreparedStatement pstmt=null;
ResultSet rs=null;
Boolean konuEkleKontrol = false;
PrintWriter out = resp.getWriter();
JSONObject j = new JSONObject();
ArrayList<String> konuEkleList = new ArrayList<String>(100);
kullaniciadi = req.getParameter("uye");
baslik = req.getParameter("baslik");
mesaj = req.getParameter("mesaj");
kategori = req.getParameter("kategori");
link = req.getParameter("link");
j.put("mesaj1",baslik);
//altKategori = req.getParameter("altkategori");
//kategoriBilgiGetir(kategori , altKategori);
String konuEkle="insert into konular(uye,baslik,mesaj,kategori,tarih,edittarih,aktif,indirimpuani,altkategori,link) values (?,?,?,?,GETDATE(),NULL,1,0,0,?)";
pstmt=con.prepareStatement(konuEkle);
pstmt.setString(1, kullaniciadi);
pstmt.setString(2, baslik);
pstmt.setString(3, mesaj);
pstmt.setString(4, kategori);
pstmt.setString(5, link);
int count = pstmt.executeUpdate();
action = (count > 0);
if (action)
{
j.put("mesaj","basarili");
konuEkleList.add(j.toString());
out.write(konuEkleList.toString());
out.close();
}
else
{
j.put("mesaj","basarisiz");
konuEkleList.add(j.toString());
out.write(konuEkleList.toString());
out.close();
}
} catch (SQLException ex) {
Logger.getLogger(KonuEkleServlet.class.getName()).log(Level.SEVERE, null, ex);
} catch (JSONException ex) {
Logger.getLogger(KonuEkleServlet.class.getName()).log(Level.SEVERE, null, ex);
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
}
}
URLs are treated as encoded when received by the servlet. The % symbol followed by a 2 hex digits is the ASCII code of the character so %50 represents the letter P. To represent the % symbol you have to send %25 to represent the % symbol.
Your URL should be:
192.168.0.222:7001/Project/KonuEkle?uye=test&&baslik=%2550 discount&&mesaj=test&&kategori=123&&link=null
Here you can find a list of the character codes:
https://www.w3schools.com/tags/ref_urlencode.asp
I've put some lines Java together and get a "programm" that read the sourcecode of a website and search line by line via BufferedReader the String Suche, copy the URL's in clipboard and give a message via JOptionPane if there is a match. All in all it work's great, but there is a problem: the perfomance.
It's mostly because of my network latency I think.
At first, here's my code:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.awt.Toolkit;
import java.awt.datatransfer.StringSelection;
import javax.swing.JOptionPane;
public class Verbindung {
static String links ="";
public static void main(String[] args) {
String[] urls = {"http://google.de"};
for(int i=0; i < urls.length; i++ ){
try {
URL url = new URL(urls[i]);
show(url);
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
Toolkit.getDefaultToolkit().getSystemClipboard().setContents(
new StringSelection(links), null);
if (links != ""){
JOptionPane.showMessageDialog(null, "Es wurden Links gefunden!");
}
}
private static void show(URL url) throws IOException{
InputStream in = url.openStream();
BufferedReader buff = new BufferedReader(new InputStreamReader(in));
String s;
while ((s = buff.readLine()) != null) {
if(s.indexOf(Suche) != -1){
links += url;
}
}
}
}
Edit:
Theres is the nice idea from RealSkeptic to insert a break and close the reading after having a match, but there isn't a big different of perfomance.
There are some more ideas to improve the perfomance of getting the Sourcecode (not including get a higher downloadspeed :D) or do it a better way?
Best regards from Germany.
I am running into following errors while trying to compile a java program on ubbuntu 12.04. . For ease of usage I have put all the classes in the same directory as the java program. I will provide the error message, the main java program as well as the class listing. I deleted the first few comment lines hence the line numbers of the error mesage do not match. I am having problem with the import statements "Scoring Request" and "ScoringResponse"
Also here is the env output
JAVA_HOME=/usr/local/java/jdk1.7.0_10
CLASSPATH=:/home/syedk/WEKA/weka-3-7-9/mysql-connector-java-3.1.17-bin.jar
Error message
=============
javac Scoring.java
Scoring.java:37: error: '.' expected
import ScoringRequest;
^
Scoring.java:37: error: ';' expected
import ScoringRequest;
^
Scoring.java:38: error: class, interface, or enum expected
import ScoringResponse;
^
3 errors
package Scoring;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.StringWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import ScoringRequest;
import ScoringResponse;
#WebServlet( name="Scoring", displayName="Scoring Servlet", urlPatterns = {"/Scoring"}, loadOnStartup=1)
public class Scoring extends HttpServlet {
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
BufferedReader reader = null;
BufferedWriter writer = null;
ScoringRequest req = null;
try {
reader = new BufferedReader(new InputStreamReader(
request.getInputStream()));
StringBuffer xml = new StringBuffer();
String line = reader.readLine();
while (line != null) {
xml.append(line);
line = reader.readLine();
}
req = new ScoringRequest(xml.toString(), null, null,
null);
ScoringResponse res = (new ScoringEngine()).score(req);
writer = new BufferedWriter(new OutputStreamWriter(
response.getOutputStream()));
writer.write(res.toXML());
writer.flush();
} catch (Exception ex) {
System.err.println(ex);
ScoringResponse res = new ScoringResponse(req.getModelName(), req.getPmmlURL(), req.getCsvInputRows(), null);
StringWriter errWriter = new StringWriter();
ex.printStackTrace(new PrintWriter(errWriter));
res.setErrorMessage(errWriter.toString());
// res.setErrorMessage(ex.getMessage());
writer = new BufferedWriter(new OutputStreamWriter(
response.getOutputStream()));
writer.write(res.toXML());
writer.flush();
} finally {
try {
if (reader != null)
reader.close();
} catch (Exception ex) {
}
try {
if (writer != null)
writer.close();
} catch (Exception ex) {
}
}
}
}
These are the problem:
import ScoringRequest;
import ScoringResponse;
You don't import classes from the default package - they're just accessible already. Just remove these two lines and it should be fine - or better, move them into a named package and import them from there. Of course you don't need to import them at all if they're in the same package as the code you're importing in.
As a side note, I would strongly recommend against naming a package the same as a class, as you are doing here:
package Scoring;
...
public class Scoring extends HttpServlet {
...
}
Aside from anything else, Scoring violated the Java conventions for package names.
I am a beginner in java programming. I am trying to read response from url and insert into database. I ran my program which is posted below and it came back with this error:
Exception in thread "main" java.lang.NullPointerException
at javaapp.JavaApplication1.parseResponseString(JavaApplication1.java:41)
at javaapp.JavaApplication1.main(JavaApplication1.java:71)
I have been trying to solve this issue on my own through research, but I could not solve it. I was wondering if anyone can help me. If you do need any other information about my program, please just ask. Thanks in advance!
And here is the code i have written
enter code here package javaapp;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;
public class JavaApplication1
{
Map<String,String> responseMap=new HashMap<String, String>();
static String input;
public void getResponseFromUrl() throws IOException
{
URL url = new URL("http://localhost:8084/home/Home");
HttpURLConnection con = (HttpURLConnection) url.openConnection();
BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream())) ;
String inputLine;
while ((inputLine = in.readLine()) != null)
{
if (inputLine.contains("<h1>"))
{
String input = inputLine;
input = input.substring(input.indexOf("<h1>") + 4, input.indexOf("</h1>"));
}
}
}
public void parseResponseString(String input)
{
String params[]=input.split(",");
for(String param:params)
{
String key= param.substring(0,param.indexOf('='));
String value= param.substring(param.indexOf('=')+1,param.length());
responseMap.put(key, value);
System.out.println(param);
}
}
public void insertToDatabase() throws SQLException, ClassNotFoundException
{
Connection conn=null;
Class.forName("oracle.jdbc.driver.OracleDriver");
conn=DriverManager.getConnection("jdbc:oracle:thin:#localhost:1521:xe","jai","jaikiran");
String insertQuery = " INSERT INTO value_1 (username1,username2,username3,username4,username5) "+
" values (?,?,?,?,?)";
PreparedStatement pstmt = conn.prepareStatement(insertQuery);
pstmt.setString(1,responseMap.get("username1"));
pstmt.setString(2,responseMap.get("username2"));
pstmt.setString(3,responseMap.get("username3"));
pstmt.setString(4,responseMap.get("username4"));
pstmt.setString(5,responseMap.get("username5"));
pstmt.executeUpdate();
}
public static void main(String[] args) throws MalformedURLException, IOException, SQLException, ClassNotFoundException
{
JavaApplication1 application =new JavaApplication1();
application.getResponseFromUrl();
application.parseResponseString(input);//shows exception here
try {
application.insertToDatabase();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
Thanks in advance,please helpme..
The static String input; is null by default and you're passing it to the parseResponseString() method.
You need to assign a value to the input variable.
I believe you wanted the assignment to be done in the getResponseFromUrl() method, where you have created a new input variable. In order to have the static String input; assigned with a value, you need to refer it with the this keyword.
if (inputLine.contains("<h1>"))
{
String input = inputLine;
this.input = input.substring(input.indexOf("<h1>") + 4, input.indexOf("</h1>"));
}
at the getResponseFromUrl method the String input is a new variable;
you must change the String input = inputLine; to String input2 = inputLine; and input = input2.substring(input.indexOf("<h1>") + 4, input.indexOf("</h1>"));
your code become :
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;
public class JavaApplication1
{
Map<String,String> responseMap=new HashMap<String, String>();
static String input;
public void getResponseFromUrl() throws IOException
{
URL url = new URL("http://localhost:8084/home/Home");
HttpURLConnection con = (HttpURLConnection) url.openConnection();
BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream())) ;
String inputLine;
while ((inputLine = in.readLine()) != null)
{
if (inputLine.contains("<h1>"))
{
String input2 = inputLine;
input = input2.substring(input.indexOf("<h1>") + 4, input.indexOf("</h1>"));
}
}
}
public void parseResponseString(String input)
{
String params[]=input.split(",");
for(String param:params)
{
String key= param.substring(0,param.indexOf('='));
String value= param.substring(param.indexOf('=')+1,param.length());
responseMap.put(key, value);
System.out.println(param);
}
}
public void insertToDatabase() throws SQLException, ClassNotFoundException
{
Connection conn=null;
Class.forName("oracle.jdbc.driver.OracleDriver");
conn=DriverManager.getConnection("jdbc:oracle:thin:#localhost:1521:xe","jai","jaikiran");
String insertQuery = " INSERT INTO value_1 (username1,username2,username3,username4,username5) "+
" values (?,?,?,?,?)";
PreparedStatement pstmt = conn.prepareStatement(insertQuery);
pstmt.setString(1,responseMap.get("username1"));
pstmt.setString(2,responseMap.get("username2"));
pstmt.setString(3,responseMap.get("username3"));
pstmt.setString(4,responseMap.get("username4"));
pstmt.setString(5,responseMap.get("username5"));
pstmt.executeUpdate();
}
public static void main(String[] args) throws MalformedURLException, IOException, SQLException, ClassNotFoundException
{
JavaApplication1 application =new JavaApplication1();
application.getResponseFromUrl();
application.parseResponseString(input);//shows exception here
try {
application.insertToDatabase();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
I think your problem is here:
String input = inputLine;
input = input.substring(input.indexOf("<h1>") + 4, input.indexOf("</h1>"));
Declaring a new variable input in the first line prevents your code to assign the value to the private field input (which is what you are probably trying to do).
Besides, the first line is useless, just remove it and use directly inputLine in the second one:
input = inputLine.substring(inputLine.indexOf("<h1>") + 4, inputLine.indexOf("</h1>"));
I can be wrong... but you define a String input as a field of your class...
I believe that you want to use in public void getResponseFromUrl() the String input field and not the local input!!
Why I say that... obviously you try in the public void parseResponseString(String input) to take the data in the input field but you don't have anything in there... so you get NullPointerException.
So what you need to change is in:
public void getResponseFromUrl() throws IOException
{
.....
/*String input = inputLine; -> wrong, because you want to use a field and not a local var... */
input = inputLine; /* now you are using the class field input */
input = input.substring...
...
}