|
Something like that, but you can easily test it.
|
|
|
|
|
hi guys,
i have a problem in connecting my web application in javaEE, when i run the servlet i have an error : java.lang.NullPointerException
and this is a screenshot for the error : http://im51.gulfup.com/TnfWrg.jpg
this the code where i try to connect to the database
public class login_servlet extends HttpServlet {
private static final long serialVersionUID = 1L;
public login_servlet() {
super();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
java.sql.Connection connexion = null;
java.sql.Statement statement = null;
ResultSet resultat = null;
String userQuery="";
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
}
try {
connexion = DriverManager.getConnection("jdbc:mysql://localhost:3306/Dlala", "IPLS", "password");
} catch (SQLException e) {
e.printStackTrace();
}
try {
statement = connexion.createStatement();
resultat = statement.executeQuery("SELECT user FROM Client;");
while (resultat.next()) {
userQuery = resultat.getString("user");
}
} catch (SQLException e) {
} finally {
try {
if (resultat != null)
resultat.close();
if (statement != null)
statement.close();
if (connexion != null)
connexion.close();
} catch (SQLException ignore) {
}
}
response.setContentType("text/html");
PrintWriter out = response.getWriter();
String n=request.getParameter("user");
String p=request.getParameter("pass");
HttpSession session = request.getSession(false);
if(session!=null)
session.setAttribute("name", n);
if( n.equals(userQuery)){
RequestDispatcher rd=request.getRequestDispatcher("home.jsp");
rd.forward(request,response);
}
else{
out.print("<p style=\"color:red\">Sorry username or password error</p>");
RequestDispatcher rd=request.getRequestDispatcher("login.jsp");
rd.include(request,response);
}
out.close();
}
}
|
|
|
|
|
So you need to use your debugger to identify where and why you have a null reference.
|
|
|
|
|
how to do pagination in java swing properly with defaultable model......i have done this but not working properly...:
please give me the code of pagination in swing? i am trying but not working properly......
|
|
|
|
|
|
Hello. I am trying to write small java bean. The classes for java beans compile successfully. When I try to use them in my .jsp file I get the error
DatabseDAO cannot be resolved to a type
Here are declarations for these beans
import java.sql.*;
import java.io.*;
import java.util.*;
public class DatabseDAO implements Serializable
{
}
import java.io.*;
public class PersonBean implements Serializable
{
}
And here is how I use them in jsp file
<%
DatabseDAO db2 = new DatabseDAO();
ArrayList persons_list = db2.GetPersons("SELECT * FROM People");
PersonBean person_bean = null;
for (int i=0; i<persons_list.size(); i++)
{
person_bean = (PersonBean) persons_list.get(i);
%>
<tr>
<td><% =person_bean.getPersonId() %></td>
<td><% =person_bean.getPersonName() %></td>
<td><% =person_bean.getPersonAddress() %></td>
</tr>
<%
}
%>
The classes (bean and DAO) are not declared in any package. What is wrong here ?
This world is going to explode due to international politics, SOON.
|
|
|
|
|
Shouldn't DatabseDAO be declared public?
|
|
|
|
|
Where is your import on .jsp page?
Should be
<%@page import="<PackageName>.DatabseDAO" %>
|
|
|
|
|
I found the answer.
modified 21-Dec-14 22:26pm.
|
|
|
|
|
|
Hello, I am doing a project for school and I cannot figure out what is wrong.
int AB = 450;
int BC = 140;
int CD = 120;
int DE = 320;
int EF = 250;
int FG = 80;
String Place1, Place2, Place3, Place4, Place5, Place6, Place7, Place8, Place9, Place10;
double MPG;
double MPH;
double Gallon;
String Trip1;
double Hour1;
int Minute1;
double Money1;
int Dist1;
String String;
Scanner scan = new Scanner(new File("C:\\Users\\Evelyn\\workspace\\ACSL\\src\\testimput"));
NumberFormat numfmt = NumberFormat.getCurrencyInstance();
NumberFormat decfmt = new DecimalFormat("00");
String delims = "[ ]+[,]";
MPG = scan.nextDouble();
Gallon = scan.nextDouble();
MPH = scan.nextDouble();
Place1 = scan.next();
Place2 = scan.next();
Place3 = scan.next();
Place4 = scan.next();
Place5 = scan.next();
Place6 = scan.next();
Place7 = scan.next();
Place8 = scan.next();
Place9 = scan.next();
Place10 = scan.next();
For the program, there is a textfile that contains exactly:
Quote: 25, 3.79, 50
A, C
E, F
C, G
B, E
A, G
However, when I attempt to run it crashes.
Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Unknown Source)
at java.util.Scanner.next(Unknown Source)
at java.util.Scanner.nextDouble(Unknown Source)
at Prog_2014.main(Prog_2014.java:34)
Does anyone know how to fix this? What am I doing wrong and what must I add.
|
|
|
|
|
You have not set the comma as a delimiter in your scanner, so it is reading it as a part of the next field.
|
|
|
|
|
Try adding the following line before the first call to nextDouble .
scan.useDelimiter("\\s*[,\\s*]\\s*");
modified 21-Dec-14 10:23am.
|
|
|
|
|
Thank you so much! That's very useful. Would you mind explaining the logic and how that statement works so I will be able to do it in the future?
|
|
|
|
|
Firstly, if you create a delimiter string (as you did in your code) you still need to apply it to the scanner so that the scanner applies it to every line it reads. The default for the scanner is to ignore all whitespace characters (as described in http://docs.oracle.com/javase/7/docs/api/java/lang/Character.html#isWhitespace(char)[^]). If that does not control the input data that you are working with then you need to set a custom delimiter, which can be a simple string of characters, or a regular expression pattern, as described in http://docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html[^].
The pattern I suggested ("\\s*[,\\s*]\\s*") breaks down as follows:
Actual pattern is: \s*[,\s*]\s*
\s* : 0 or more whitespace characters (e.g. spaces, tabs etc)
[,\s*] : a comma or 0 or more whitespace characters
\s* : 0 or more whitespace characters (e.g. spaces, tabs etc)
I'm not an expert in RegEx patterns so I am sure this can be improved upon. But what you have in your input file is:
25.0, 37.1, 10
So the the basic pattern is:
0 or more whitespace characters
0 or more comma characters
0 or more whitespace characters
a number
0 or more whitespace characters
|
|
|
|
|
Hello. I am trying to authenticate input from databse using JDBC and then redirect the user to another page accordingly. Here is my code for small servlet that does authentication
public class ServletAuthUser extends HttpServlet
{
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
try
{
String sUserName = request.getParameter("username");
String sPassword = request.getParameter("password");
String sURL = "Jdbc:Odbc:ServletsDB_DSN";
String sForName = "sun.jdbc.odbc.JdbcOdbcDriver";
MyDatabase db = new MyDatabase(sURL, sForName);
boolean bIsUserValid = (sUserName.equals("admin") && sPassword.equals("admin")) ? true : false;
db.CloseConnection();
if(bIsUserValid == true)
response.sendRedirect("welcome.html");
else
response.sendRedirect("error.html");
}
And here is my welcome page.
<html>
<head>
<title>Welcome</title>
</head>
<body>
<h1>Welcome ADMIN.</h1>
</body>
</html>
I have tried recompiling the servlets and then restarted the apache server several times but my login servlets still shows me the old output that I tried little ago (another servlet which generates data using JDBC). Why is it still showing the same old output despite recompiling the servlet and restarting the server? Please ask for any info if you need. Thanks for any input.
EDIT
I was not writing the deployment descriptor properly.
modified 19-Dec-14 14:13pm.
|
|
|
|
|
Are you sure that you have copied the new servlet to the server correctly?
|
|
|
|
|
Hello friends,
I am new to Java having understanding of the basics. But, I have given a mini project to make the Login Frame on the eclipse platform and to verify the user login details and thereby showing his access rights on the other frame. I have to use Derby database for it.
I am really confused as I have to submit it in next 3 days
It would be kind if anyone can help me.
Thanks
|
|
|
|
|
It is unlikely that any response in this forum would be enough to help you, if you don't understand how to create a simple application such as you describe. For a start you will need to design your database and learn how to access it in Java; take a look at http://docs.oracle.com/javase/tutorial/jdbc/index.html[^]. As for the login screen, that is just a matter of adding a few labels, text boxes and buttons to a JFrame and accessing the information provided by the user. There are many useful samples in http://www.codeproject.com/KB/java/[^].
|
|
|
|
|
thnks Richard
|
|
|
|
|
What have you achieved so far?
The Eclipse IDE is a Eclipse RCP Application.
You can easily exchange the original login and build your thingy as a Eclipse RCP Plugin.
Lars Vogel has a lot of really good tutorials for you:
http://www.vogella.com/tutorials/EclipseRCP/article.html[^]
regards Torsten
I never finish anyth...
|
|
|
|
|
what is interface ?and why it is used?
|
|
|
|
|
|
|
Hello. I am implementing action event in a class 1, database handling in class 2 while all the controls are instantiated in a 3rd class. Heirarchy becomes like this.
class MyDatabase
{
}
class ActionHandler implements ActionListener
{
MyDatabase db = null;
View m_View = null;
public ActionHandler(View view)
{
m_View = view;
db = new MyDatabase();
}
public void actionPerformed(ActionEvent ae){
if(ae.getSource() == MyView.AddButton)
db.AddRecord();
}
}
class View
{
ActionHandler action_handler = null;
public View()
{
action_handler = new ActionHandler(this);
AddButton.addActionListener(action_handler);
}
}
Question is: how do I pass my JTextField values from View class to ActionHandler class ? I don't think passing these values in the constructor of ActionHandler is good idea.
|
|
|
|