Here is my Method
public synchronized void updateCompanyByNameandUrl(String companyName, String companyUrl)
{
PreparedStatement ps = null;
PreparedStatement ps1 = null;
Connection con = null;
Connection globalconnection = null;
String updateCompanyquery = "update `anjo_company_master` set URL = ? where COMPANY_NAME=?";
String updateGlobalCompQuery= "update `anjo_company_master_global` set URL = ?, USER_IP = ? where COMPANY_NAME=?";
try
{
***InetAddress localhost = InetAddress.getLocalHost();
String userIp = localhost.getHostAddress().trim();***
con = SQLHikariConnectionFactory.getInstance().getConnection();
ps = con.prepareStatement(updateCompanyquery);
ps.setString(1, companyUrl.trim());
ps.setString(2, companyName.trim());
ps.executeUpdate();
//Updating companyUrl in Global Master also
globalconnection = connFactory.getGlobalCompanyConnection();
ps1 = globalconnection.prepareStatement(updateGlobalCompQuery);
ps1.setString(1, companyUrl.trim());
ps1.setString(2, userIp.trim());
ps1.setString(3, companyName.trim());
ps1.executeUpdate();
}
catch(Exception e)
{
e.printStackTrace();
LOG.info(e.getMessage(), e);
}
finally
{
try {
if (con != null)
con.close();
if (ps != null)
ps.close();
}
catch (Exception e2)
{
e2.printStackTrace();
}
}
}
Here because of
InetAddress localhost = InetAddress.getLocalHost();
String userIp = localhost.getHostAddress().trim();
I didn't get userIp I got serverIp
But I get ServerIP not Client IP Can anyone help me?
please set the HttpServletRequest
object into the DAO class before calling the DAO method.
Use the below code.
Class Test extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("Client IP " + request.getHeader("X-Forwarded-For"));
TestDAO dao = TestDAOImpl();
dao.setHttpServletRequest(request);
dao.xxxxx(xx,xx)\\call your method like this
}
}
Class TestDAOImpl implements TestDAO {
private HttpServletRequest request;
public void setHttpServletRequest(HttpServletRequest request) {
this.request = request;
}
//write your method here.
public <?> doSomthing(){
String clientIP= this.request.getHeader("X-Forwarded-For");
//d
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.