Does anyone know how can I call HttpServlet or VaadinServlet on a button click?
The URL for UI is localhost:8181/OnlineAccounting/
I am able to call servlet by manually entering URL localhost:8181/OnlineAccounting/download but I want to achieve it with clicking a button.
package com.example.Reports;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
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 net.sf.jasperreports.engine.JRResultSetDataSource;
import net.sf.jasperreports.engine.JasperCompileManager;
import net.sf.jasperreports.engine.JasperExportManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperReport;
import net.sf.jasperreports.engine.JasperRunManager;
import net.sf.jasperreports.view.JasperViewer;
import com.example.Connection.Connect;
import com.vaadin.server.VaadinServlet;
#WebServlet("download")
public class download extends HttpServlet {
private static final long serialVersionUID = 1L;
public download() {
super();
// TODO Auto-generated constructor stub
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
Connect c=new Connect("{Call chartofaccounts()}", null);
JRResultSetDataSource DataSet=new JRResultSetDataSource(c.rs);
try{
JasperReport JReport=JasperCompileManager.compileReport("C:\\Users\\mrreh_000\\Desktop\\Jasperreport\\MyReports\\Blank_A4_2.jasper");
JasperPrint jprint;
jprint = JasperFillManager.fillReport(JReport,null,DataSet);
JasperViewer.viewReport(jprint,false);
/// String source="C:\\Users\\mrreh_000\\Desktop\\Jasperreport\\MyReports\\Blank_A4.jrxml";
//String query="{Call chartofaccounts()}";
// JReporting jReporting=new JReporting(source,query);
String serverHomeDir = System.getenv("CATALINA_HOME");
String reportDestination = serverHomeDir +"\\report.pdf";
JasperExportManager.exportReportToPdfFile(jprint, reportDestination);
FileInputStream fis = new FileInputStream(new File(reportDestination));
// Fast way to copy a bytearray from InputStream to OutputStream
org.apache.commons.io.IOUtils.copy(fis, response.getOutputStream());
response.setContentType("application/pdf");
response.setHeader("Content-Disposition", "attachment; filename=" + reportDestination);
response.flushBuffer();
}catch(Exception e){System.out.println(e);}
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
}
}
Here a similar question on stackexchange.
I suggest that you also add a random part to your report filename (or the report path), otherwise the webbrowser will not redownload the report when it is already cached from a previous download.
Related
I am trying to take a test driven development approach to building a Java based app running on App Engine, but I am having difficulties getting the setup working.
My servlet
package mobi.grocerymonkey.groceryapp;
import com.google.appengine.api.utils.SystemProperty;
import java.io.IOException;
import java.io.BufferedReader;
import java.util.Properties;
import org.json.JSONObject;
import java.util.logging.Logger;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.ServletException;
import static com.googlecode.objectify.ObjectifyService.ofy;
import com.googlecode.objectify.ObjectifyService;
/* This is the servlet */
#WebServlet(name = "GroceryServlet", value = "/grocery")
public class GroceryServlet extends HttpServlet {
private static final Logger log = Logger.getLogger(GroceryServlet.class.getName());
#Override
public void init() throws ServletException {
log.info("context init");
ObjectifyService.init();
ObjectifyService.register(Grocery.class);
}
#Override
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws IOException {
response.setContentType("text/plain");
response.getWriter().println("Hello Kitty");
}
#Override
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws IOException {
BufferedReader reader = request.getReader();
String line = null;
StringBuffer stringBuffer = new StringBuffer();
while((line = reader.readLine()) != null) {
stringBuffer.append(line);
}
String jsonString = stringBuffer.toString();
JSONObject json = new JSONObject(jsonString);
log.info("JSON "+ jsonString);
Grocery grocery = new Grocery();
grocery.setName((String) json.get("name"));
grocery.setQuantity((Integer) json.get("quantity"));
ofy().save().entity(grocery).now();
log.info("JSON name "+ grocery.getName());
response.setContentType("application/json");
response.getWriter().println(jsonString);
}
}
web.xml file
<?xml version="1.0" encoding="utf-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1">
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
<filter>
<filter-name>ObjectifyFilter</filter-name>
<filter-class>com.googlecode.objectify.ObjectifyFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>ObjectifyFilter</filter-name>
<url-pattern>/*</url-pattern>
<dispatcher>REQUEST</dispatcher>
<dispatcher>INCLUDE</dispatcher>
<dispatcher>FORWARD</dispatcher>
</filter-mapping>
</web-app>
My unit test
package mobi.grocerymonkey.groceryapp;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Mockito.when;
import static org.mockito.Mockito.mock;
import com.google.appengine.tools.development.testing.LocalServiceTestHelper;
import com.google.appengine.tools.development.testing.LocalDatastoreServiceTestConfig;
import com.google.cloud.datastore.DatastoreOptions;
import com.google.cloud.datastore.DatastoreOptions;
import com.googlecode.objectify.ObjectifyFactory;
import com.googlecode.objectify.ObjectifyService;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.json.JSONObject;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.io.BufferedReader;
import java.io.StringReader;
import java.io.Reader;
import java.io.Closeable;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Unit tests for {#link HelloAppEngine}.
*/
#RunWith(JUnit4.class)
public class GroceryServletTest {
private static final String MOCK_URL = "/grocery";
// Set up a helper so that the ApiProxy returns a valid environment for local testing.
private final LocalServiceTestHelper helper =
new LocalServiceTestHelper(new LocalDatastoreServiceTestConfig());
private Closeable closeable;
#Mock
private HttpServletRequest mockRequest;
#Mock
private HttpServletResponse mockResponse;
private StringWriter responseWriter;
private GroceryServlet servletUnderTest;
#Before
public void setUp() throws Exception {
MockitoAnnotations.initMocks(this);
helper.setUp();
ObjectifyService.init(new ObjectifyFactory(
DatastoreOptions.newBuilder()
.setHost("http://localhost:8081")
.setProjectId("enduring-trees-259812")
.build()
.getService()
));
closeable = ObjectifyService.begin();
// Set up some fake HTTP requests
when(mockRequest.getRequestURI()).thenReturn(MOCK_URL);
JSONObject grocery = new JSONObject();
grocery.put("name", "Beer");
Reader inputString = new StringReader(grocery.toString());
BufferedReader reader = new BufferedReader(inputString);
when(mockRequest.getReader()).thenReturn(reader);
// Set up a fake HTTP response.
responseWriter = new StringWriter();
when(mockResponse.getWriter()).thenReturn(new PrintWriter(responseWriter));
servletUnderTest = new GroceryServlet();
servletUnderTest.init();
}
#After public void tearDown() throws Exception {
closeable.close();
helper.tearDown();
}
#Test
public void doGetWritesResponse() throws Exception {
servletUnderTest.doGet(mockRequest, mockResponse);
// We expect our hello world response.
assertThat(responseWriter.toString())
.contains("Hello Kitty");
}
#Test
public void doPostWritesResponse() throws Exception {
JSONObject reqObj = new JSONObject();
reqObj.put("name", "Beer");
reqObj.put("quantity", 5);
StringReader reader = new StringReader(reqObj.toString());
when(mockRequest.getReader()).thenReturn(new BufferedReader(new StringReader(reqObj.toString())));
servletUnderTest.doPost(mockRequest, mockResponse);
// We expect our hello world response.
assertThat(responseWriter.toString())
.contains(reqObj.getString("name"));
}
}
The test fails with the following error message
[ERROR] Tests run: 2, Failures: 0, Errors: 1, Skipped: 0, Time
elapsed: 0.103 s <<< FAILURE! - in
mobi.grocerymonkey.groceryapp.GroceryServletTest [ERROR]
doPostWritesResponse(mobi.grocerymonkey.groceryapp.GroceryServletTest)
Time elapsed: 0.078 s <<< ERROR! java.lang.IllegalStateException: You
have not started an Objectify context. You are probably missing the
ObjectifyFilter. If you are not running in the context of an http
request, see the ObjectifyService.run() method. at
mobi.grocerymonkey.groceryapp.GroceryServletTest.doPostWritesResponse(GroceryServletTest.java:109)
which is caused by this line ofy().save().entity(grocery).now() in my servlet. When I remove it, the test is run without errors.
I have tried to follow different approaches to resolve this error found here on stackoverflow but without luck.
How should the test/app be setup to be able to develop it using a test driven approach? What I am looking for is a way to be able to write the unit test first and then the actual application. But how to succeed?
(Disclaimer, I haven't worked with Java in over a decade)
UPDATE
ServletContext file
package mobi.grocerymonkey.groceryapp;
import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;
import java.io.Closeable;
import java.io.IOException;
import com.googlecode.objectify.ObjectifyService;
import static com.googlecode.objectify.ObjectifyService.ofy;
#WebListener
public class GroceryContextListener implements ServletContextListener {
private ServletContext context;
private Closeable closeable;
public void contextInitialized(ServletContextEvent event) {
this.context = event.getServletContext();
ObjectifyService.init();
this.closeable = ObjectifyService.begin();
ObjectifyService.register(Grocery.class);
System.out.println("Context initialized");
}
public void contextDestroyed(ServletContextEvent event) {
try {
this.closeable.close();
} catch(IOException ioe) {
}
}
}
Unittest file
package mobi.grocerymonkey.groceryapp;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Mockito.when;
import static org.mockito.Mockito.mock;
import com.google.appengine.tools.development.testing.LocalServiceTestHelper;
import com.google.appengine.tools.development.testing.LocalDatastoreServiceTestConfig;
import com.google.cloud.datastore.DatastoreOptions;
import com.google.cloud.datastore.DatastoreOptions;
import com.googlecode.objectify.ObjectifyFactory;
import com.googlecode.objectify.ObjectifyService;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.json.JSONObject;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.io.BufferedReader;
import java.io.StringReader;
import java.io.Reader;
import java.io.Closeable;
import java.io.IOException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.ServletContext;
import javax.servlet.ServletContextListener;
import javax.servlet.ServletContextEvent;
/**
* Unit tests for {#link HelloAppEngine}.
*/
#RunWith(JUnit4.class)
public class GroceryServletTest {
private static final String MOCK_URL = "/grocery";
// Set up a helper so that the ApiProxy returns a valid environment for local testing.
private final LocalServiceTestHelper helper =
new LocalServiceTestHelper(new LocalDatastoreServiceTestConfig());
private Closeable closeable;
#Mock
private HttpServletRequest mockRequest;
#Mock
private HttpServletResponse mockResponse;
private ServletContextListener contextListener;
private ServletContext context;
private StringWriter responseWriter;
private GroceryServlet servletUnderTest;
#Before
public void setUp() throws Exception {
MockitoAnnotations.initMocks(this);
helper.setUp();
contextListener = new GroceryContextListener();
context = mock(ServletContext.class);
// Set up some fake HTTP requests
when(mockRequest.getRequestURI()).thenReturn(MOCK_URL);
JSONObject grocery = new JSONObject();
grocery.put("name", "Beer");
Reader inputString = new StringReader(grocery.toString());
BufferedReader reader = new BufferedReader(inputString);
when(mockRequest.getReader()).thenReturn(reader);
// when(mockRequest.getServletContext()).thenReturn(context);
// Set up a fake HTTP response.
responseWriter = new StringWriter();
when(mockResponse.getWriter()).thenReturn(new PrintWriter(responseWriter));
servletUnderTest = new GroceryServlet();
}
#After
public void tearDown() throws Exception {
helper.tearDown();
}
#Test
public void doGetWritesResponse() throws Exception {
servletUnderTest.doGet(mockRequest, mockResponse);
// We expect our hello world response.
assertThat(responseWriter.toString())
.contains("Hello Kitty");
}
#Test
public void doPostWritesResponse() throws Exception {
contextListener.contextInitialized(new ServletContextEvent(context));
JSONObject reqObj = new JSONObject();
reqObj.put("name", "Beer");
reqObj.put("quantity", 5);
StringReader reader = new StringReader(reqObj.toString());
when(mockRequest.getReader()).thenReturn(new BufferedReader(new StringReader(reqObj.toString())));
servletUnderTest.doPost(mockRequest, mockResponse);
// We expect our hello world response.
assertThat(responseWriter.toString())
.contains(reqObj.getString("name"));
}
}
Servlet file
package mobi.grocerymonkey.groceryapp;
import com.google.appengine.api.utils.SystemProperty;
import java.io.IOException;
import java.io.BufferedReader;
import java.util.Properties;
import org.json.JSONObject;
import java.util.logging.Logger;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.ServletException;
import static com.googlecode.objectify.ObjectifyService.ofy;
import com.googlecode.objectify.ObjectifyService;
/* This is the servlet */
#WebServlet(name = "GroceryServlet", value = "/grocery")
public class GroceryServlet extends HttpServlet {
private static final Logger log = Logger.getLogger(GroceryServlet.class.getName());
#Override
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws IOException {
response.setContentType("text/plain");
response.getWriter().println("Hello Kitty");
}
#Override
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws IOException {
BufferedReader reader = request.getReader();
String line = null;
StringBuffer stringBuffer = new StringBuffer();
while((line = reader.readLine()) != null) {
stringBuffer.append(line);
}
String jsonString = stringBuffer.toString();
JSONObject json = new JSONObject(jsonString);
log.info("JSON "+ jsonString);
Grocery grocery = new Grocery();
grocery.setName((String) json.get("name"));
grocery.setQuantity((Integer) json.get("quantity"));
ofy().save().entity(grocery).now();
log.info("JSON name "+ grocery.getName());
response.setContentType("application/json");
response.getWriter().println(jsonString);
}
}
Now I am getting a "com.google.cloud.datastore.DatastoreException: Unauthenticated" error when running the test, so looks like I am on the right way. Would I store the datastore credentials in the web.xml and then pass them to the context similar to
ObjectifyService.init(new ObjectifyFactory(
DatastoreOptions.newBuilder()
.setHost("http://localhost:8081")
.setProjectId("enduring-trees-259812")
.build()
.getService()
));
ObjectifyService.factory().register(Grocery.class);
New Update
I upgraded to Junit5 and rewrote the entire test to this
package mobi.grocerymonkey.groceryapp;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.AfterAll;
import static org.junit.jupiter.api.Assertions.assertEquals;
import com.googlecode.objectify.ObjectifyFactory;
import com.googlecode.objectify.ObjectifyService;
import com.googlecode.objectify.util.Closeable;
import static com.googlecode.objectify.ObjectifyService.factory;
import static com.googlecode.objectify.ObjectifyService.ofy;
import com.googlecode.objectify.Key;
import com.google.cloud.datastore.Datastore;
import com.google.cloud.datastore.DatastoreOptions;
import com.google.cloud.datastore.testing.LocalDatastoreHelper;
import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.tools.development.testing.LocalDatastoreServiceTestConfig;
import com.google.appengine.tools.development.testing.LocalServiceTestHelper;
import mobi.grocerymonkey.groceryapp.util.TestBase;
import mobi.grocerymonkey.groceryapp.domain.Grocery;
import mobi.grocerymonkey.groceryapp.domain.GroceryList;
public class MyFirstTest extends TestBase {
// Maximum eventual consistency.
private final static LocalServiceTestHelper helper =
new LocalServiceTestHelper(new LocalDatastoreServiceTestConfig()
.setDefaultHighRepJobPolicyUnappliedJobPercentage(100));
Closeable closeable;
#BeforeAll
public static void setUp() {
helper.setUp();
}
#AfterAll
public static void tearDown() {
helper.tearDown();
}
#BeforeEach
public void setUpEach() {
ObjectifyService.init(new ObjectifyFactory(
DatastoreOptions.getDefaultInstance().getService()));
closeable = ObjectifyService.begin();
}
#AfterEach
public void tearDownEach() {
closeable.close();
}
#DisplayName("Test MyFirstTest.testAddition()")
#Test
public void testAddition() {
assertEquals(1 + 1, 2);
}
#DisplayName("Testing testGroceryList()")
#Test
public void testGroceryList() {
factory().register(GroceryList.class);
GroceryList list = new GroceryList("Weekend Beer List");
Key<GroceryList> k1 = ofy().save().entity(list).now();
assertEquals(1+1, 2);
}
}
It is deliberately kept in a single file for now. But for some reason, the datastore cannot find the emulator that is running when the test is run. I am getting a Datastore Unauthenticated error.
I ran gcloud beta emulators datastore start and $(gcloud beta emulators datastore env-init) before running the unit test.
The problem is that you're calling ObjectifyService.init() twice, but you only called begin() on the first (abandoned) factory.
You call init() in your setUp() method, which initializes the static ObjectifyFactory. You then open a session on that factory with the ObjectifyService.begin() call.
At the end of your setUp() you call servletUnderTest.init(), which also calls ObjectifyService.init(). This replaces the static ObjectifyFactory. When you next execute your servlet and call ofy()..., you're using a factory that has not started a session.
Take a look at the code for ObjectifyService. It's quite literally just a few lines of code to wrap a static ObjectifyFactory instance.
If you have more than one servlet, this code will not work well in production either - you only want to initialize and register your classes once. I recommend doing this with a ServletContextListener.
The core of test-driven development revolves around five steps, which you repeated throughout the software development life cycle.
Test-driven development life-cycle:
Write the test
Run the test (without implementation code, test does not pass)
Write just enough implementation so the test passes
Run all tests (test pass)
Refactor
Repeat
By following this steps you can create a TDD implementation for your application.
There is no specific way for Google Cloud to do beside the steps I have specified above.
As specified in your error, you can see that you have not started an Objectify context, and you are missing the ObjectifyFilter.
Here is a implementation of a list in Java, which follows the TDD, which may be helpful to clear some of your concerns.
I don't have permission to save files to the filesystem. All the examples I could find online are using filesystem.
I want to create workbook in memory as below:
HSSFWorkbook workbook = new HSSFWorkbook();
Write data to workbook...
Make the workbook password protected.?
Then write the workbook to output stream as below:
workbook.write(servlet.getOutputStream());
I was able to get the following to work with an HttpServlet and HSSFWorkbook .xls file - WorkbookServlet.java:
package com.joshden;
import java.io.IOException;
import java.io.OutputStream;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.poi.hssf.record.crypto.Biff8EncryptionKey;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Workbook;
#WebServlet("/workbook")
public class WorkbookServlet extends HttpServlet {
private static final long serialVersionUID = 4087954595439224462L;
private static final String password = "foobar1";
#Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
response.setContentType("application/vnd.ms-excel");
response.addHeader("Content-Disposition", "attachment; filename=password_protected.xls");
createAndWriteEncryptedWorkbook(response.getOutputStream());
}
private void createAndWriteEncryptedWorkbook(OutputStream requestOutputStream) throws IOException {
HSSFWorkbook workbook = new HSSFWorkbook();
populateWorkbook(workbook);
Biff8EncryptionKey.setCurrentUserPassword(password);
workbook.write(requestOutputStream);
workbook.close();
}
private void populateWorkbook(Workbook workbook) {
workbook.createSheet("TestSheet");
// TODO create other sheets, populate cell values
}
}
With requests to /workbook, the servlet creates the HSSF workbook in memory with a sheet named TestSheet. The password is set to "foobar1". It prompts the browser to download it as a file with the name password_protected.xls.
Also see Apache POI - Encryption support. Encryption for the binary formats (HSSF) has only been available since Apache POI 3.16. At the time of this writing, 3.16 hasn't been released yet, and I used 3.16-beta1 in my example.
The encyprtion support guide also has info for creating encrypted (password protected) .xlsx (XSSF) files. Here's an example I created that will prompt the file to be named password_protected.xlsx:
package com.joshden;
import java.io.IOException;
import java.io.OutputStream;
import java.security.GeneralSecurityException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.poi.openxml4j.opc.OPCPackage;
import org.apache.poi.poifs.crypt.EncryptionInfo;
import org.apache.poi.poifs.crypt.EncryptionMode;
import org.apache.poi.poifs.crypt.Encryptor;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
#WebServlet("/workbook")
public class WorkbookServlet extends HttpServlet {
private static final long serialVersionUID = 4087954595439224462L;
private static final String password = "foobar1";
#Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
response.addHeader("Content-Disposition", "attachment; filename=password_protected.xlsx");
createAndWriteEncryptedWorkbook(response.getOutputStream());
}
private void createAndWriteEncryptedWorkbook(OutputStream requestOutputStream) throws IOException {
XSSFWorkbook workbook = new XSSFWorkbook();
OPCPackage opc = workbook.getPackage();
populateWorkbook(workbook);
try {
POIFSFileSystem fileSystem = new POIFSFileSystem();
opc.save(getEncryptingOutputStream(fileSystem, password));
fileSystem.writeFilesystem(requestOutputStream);
}
finally {
workbook.close();
}
}
private void populateWorkbook(Workbook workbook) {
workbook.createSheet("TestSheet");
// TODO create other sheets, populate cell values
}
private OutputStream getEncryptingOutputStream(POIFSFileSystem fileSystem, String password) throws IOException {
EncryptionInfo encryptionInfo = new EncryptionInfo(EncryptionMode.agile);
Encryptor encryptor = encryptionInfo.getEncryptor();
encryptor.confirmPassword(password);
try {
return encryptor.getDataStream(fileSystem);
}
catch (GeneralSecurityException e) {
// TODO handle this better
throw new RuntimeException(e);
}
}
}
I want to get current country name in java . I use this code.
private HttpServletRequest request;
Locale currentCountry = request.getLocale();
System.out.println(currentCountry.getDisplayCountry());
But it through an NullPointerException like Caused by: java.lang.NullPointerException. at 2nd line. Please Help me anyone.
The snippet of code is not enough to tell why you got the Null Pointer. I assume you are use servlet on server side.
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Enumeration;
import java.util.Locale;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.HttpServletBean;
public class MyServlet extends HttpServletBean {
private static final long serialVersionUID = 1L;
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException {
PrintWriter out = response.getWriter();
Locale userlocale = request.getLocale();
out.println("Your Country : " + userlocale.getCountry());
out.println();
out.println("");
}
}
i created a jsp page that calls another jsp page that show an image took from a blob field in my mysql db:
<img src="blob.jsp">
it works. But, somewhere in this forum i read that this is not the right way to do it. I should, instead, using a servlet this way:
<img src="servlet_name">
I created a servlet, but it doesent show me the image it shows me this
ÿØÿàJFIFHHÿí$Photoshop 3.08BIMí ResolutionHH8BIM FX Global Lighting Anglex8BIMFX Global Altitude8BIMó Print Flags 8BIM Copyright Flag8BIM'Japanese Print Flags 8BIMõColor Halftone SettingsH/fflff/ff¡™š2Z5-8BIMøColor Transfer Settingspÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿèÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿèÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿèÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿè8BIM Layer State8BIM Layer Groups8BIMGuides##8BIM URL overrides8BIMSlicesuƒD Untitled-1Dƒ8BIMICC Untagged Flag
This is my simple servlet
package Jeans;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.sql.Blob;
import java.sql.ResultSet;
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("/BlobDisplay")
public class BlobDisplay extends HttpServlet {
private static final long serialVersionUID = 1L;
GestioneDB gestioneDB ;
public BlobDisplay() {
super();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
Blob image = null;
byte[ ] imgData = null ;
String query = null;
query = request.getParameter(query);
gestioneDB = new GestioneDB();
ResultSet rs = gestioneDB.rs("select immagine_principale from news where id ='217'");
try{
if (rs.next()) {
image = rs.getBlob("immagine_principale");
imgData = image.getBytes(1,(int)image.length());
response.setContentType("image/jpg");
OutputStream o = response.getOutputStream();
o.write(imgData);
o.flush();
o.close();
}
}
catch(Exception e){}
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doGet(request,response);
}
}
Here is a debugging hint for now.
JSPs are just "inside out" servlets and they are translated to servlets by the container. Since your JSP works and your servlet doesn't, why don't you check out (and post) the generated servlet. If you are using tomcat, it would be in a file called blob__jsp.java deep in the work directory. Then compare the calls and set up of your servlet and the generated servlet.
(My first guess is the content type, but you seem to be setting that)
I have downloaded the log4j jar.
But dont know how to use it to save the output to a text file.
My code is:
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Enumeration;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.log4j.Logger;
public class ShowHeaders extends HttpServlet {
/**
*
*/
private static final long serialVersionUID = 1L;
String headerName;
String headerValue;
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException {
response.setContentType("text/plain");
PrintWriter out = response.getWriter();
String method= request.getMethod();
out.println("Request Method: " +method);
out.println("-----------------------");
String uri= request.getRequestURI();
out.println("URI: " +uri);
out.println("-----------------------");
out.println("Request Headers");
Enumeration<?> Enumeration = (java.util.Enumeration<?>) request.getHeaderNames();
while (Enumeration.hasMoreElements()) {
String headerName = (String) Enumeration.nextElement();
String headerValue = request.getHeader(headerName);
out.print(""+headerName + ": ");
out.println(headerValue + "");
}
}
Kindly tell me where to describe the file location & what to use instead of out.println(); to display it that text file
Well, you should take a look at the log4j tutorial. It is really good !
this code should give you a headstart :
public static void main(String[] args) {
Logger X = Logger.getLogger("com.foo");
try {
X.addAppender(new FileAppender(new PatternLayout("%d %-5p [%c{1}] %m%n"),"src/test.log"));
} catch (IOException e) {
e.printStackTrace();
}
X.debug("Hello World debug message");
}
The log4j tutorial explains how to configure logging, and includes example configs that log to a file. (Search for where it says "Here is another configuration file that uses multiple appenders". That "config" includes file appenders.)